Programmatically uploading and setting permission to a document in a document library in SharePoint

We can use the following code for uploading a document to document library.

// create a filestream object and open an existing file for reading its contents

FileStream oFileStream = File.OpenRead(@”C:\G.doc”);

byte[] content = new byte[oFileStream.Length];

// store the contents in a byte array

oFileStream.Read(content, 0, (int)oFileStream.Length);


// create an SPSIte object

using (SPSite objSite = new SPSite(“http://d-1246”))


// get reference to the web application or web site

using (SPWeb objWeb = objSite.OpenWeb())


// get reference to the document library. Proposal – name of the document library

SPFolder mylibrary = objWeb.Folders[“Proposal”];

objWeb.AllowUnsafeUpdates = true;

// add the file to the document library and update it

SPFile myFile = mylibrary.Files.Add(“MyProposal.doc”, content, true);


// get the reference to the file just uploaded as a SPListItem for setting permissions

SPListItem myListItem = myFile.Item;

// HasUniqueRoleAssignments – returns false if it inherits permission from parent else true


// BreakRoleInheritance – breaking role inheritance so that we can define our own permissions


// webroledefinitions – Full Right, Design, Contribute and Read

SPRoleDefinitionCollection webroledefinitions = objWeb.RoleDefinitions;

// group – different groups created in that site

SPGroup group = objWeb.SiteGroups[“MyGroup”];

SPRoleAssignment roleassignment = new SPRoleAssignment(group);



// same for the user as well

SPUser user = objWeb.SiteUsers[@”domainname\username”];

SPRoleAssignment roleassignment1 = new SPRoleAssignment(user);

roleassignment1.RoleDefinitionBindings.Add(webroledefinitions[“Full Control”]);





Delete a SSP (Shared Service Provider)

To delete a SSP we need to do the following in the cmd prompt

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN> stsadm -o deletessp -title “shared service name” -deletedatabases

For deleting default ssp first we need to delete all the web application associated with it or associate them with some other shared services !!

%d bloggers like this: