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);
oFileStream.Close();
// 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);
mylibrary.Update();
// 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
myListItem.HasUniqueRoleAssignments.ToString());
// BreakRoleInheritance – breaking role inheritance so that we can define our own permissions
myListItem.BreakRoleInheritance(false);
// 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);
roleassignment.RoleDefinitionBindings.Add(webroledefinitions[“Design”]);
myListItem.RoleAssignments.Add(roleassignment);
// same for the user as well
SPUser user = objWeb.SiteUsers[@”domainname\username”];
SPRoleAssignment roleassignment1 = new SPRoleAssignment(user);
roleassignment1.RoleDefinitionBindings.Add(webroledefinitions[“Full Control”]);
myListItem.RoleAssignments.Add(roleassignment1);
}
}
Bye
Discover more from Nishant Rana's Weblog
Subscribe to get the latest posts sent to your email.

Hi,
Im looking out for a script which
automates the archival policy processes.
I would have a dump of documents and an excel sheet provided with the following columns:
Old File Name
New File Name
Folder Name (where files need to be moved)
Ad–ID to provide access at files
Script should read the information from the excel sheet to perform the following actions into WSS:
· Re-naming of files as per the new file name mentioned in the sheet.
· Movement of files to a particular folder as per the folder name mentioned in the sheet.
· Automatic access to the files as per the Ad-Id mentioned in the sheet.
System should validate the access of the document for each user.
I would be grateful if you could assist me on this
Thanks & Regards,
narayanan rm.
LikeLike
The topic is to much informative but what we have to do if i have a document already uploded..and i want a refrence a that file i.e abc.doc.Please help me if someone have knowledge about it.
Thanks in Advance
LikeLike
thank you so much. The post was helpgful. I have another question. I have a folder and a subfolder inside that which is hiiden and the file inside the subfolder. I need to access the worflow status column of the file and update it in the folder workflow column. Any help in this is appreciated
LikeLike
thank u …..it solve my problem
nice post
cheeers
LikeLike
Excellent and perfect post !!!
LikeLike
Very good example. I was to use it as is. Works great! Thanks for sharing.
LikeLike
Thanks for your post.I want to know if it is possible to create application from sharepoint that can integrate outlook 2007 by creating alert programatically.
LikeLike
xd
LikeLike
This is the easiest way of usage, but a fairly good example.
Suggest to extend this with using SPItemEvent, f.e. ItemUploaded, add som extra, f.e. RunWithElevated privileges and AD group handling, not just trivial SPGroup.
This way the sample would be more realistic.
LikeLike
by applying this method permissions are set on item not on document , if user who has read permission can download document , edit the document and saved the changes to server ,
LikeLike