// Create a folder under the root directory of the List
Public static string creatfoldertospdoclib (string strfoldername, string strdoclibname)
{
String folderpath = string. empty;
Try
{
Using (spsite site = new spsite (siteurl ))
{
Using (spweb web = site. openweb ())
{
Web. allowunsafeupdates = true;
Splistcollection lists = web. getlistsoftype (spbasetype. documentlibrary );
Lists. includerootfolder = true;
Splist list = lists [strdoclibname];
List. enablefoldercreation = true;
Splistitem item = List. Items. Add (list. rootfolder. serverrelativeurl, spfilesystemobjecttype. folder, strfoldername );
Item. Update ();
List. Update ();
Folderpath = item ["fileref"]. tostring ();
Web. allowunsafeupdates = false;
}
}
}
Catch
{
}
Return folderpath;
}
// Upload a file to a folder and grant it to the relevant user
Public static bool uploadfiletofolder (byte [] filestream, string filename, string folderpath, string allloginname)
{
Try
{
Using (spsite site = new spsite (siteurl ))
{
Using (spweb web = site. openweb ())
{
Web. allowunsafeupdates = true;
Spfolder folder = web. getfolder (folderpath );
Splistitem listitem = folder. Files. Add (filename, filestream). item;
// Disconnect the permissions inherited from the original list items so that they can set independent Permissions
Listitem. breakroleinheritance (true );
// Remove the original inherited Permissions
Foreach (sproleassignment roleassignment in listitem. roleassignments)
{
Roleassignment. roledefinitionbindings. removeall ();
Roleassignment. Update ();
Listitem. Update ();
}
// Obtain the user whose permissions are to be set
Spuser myuser = web. ensureuser (allloginname );
// Define permission allocation
Sproleassignment myroleassignment = new sproleassignment (myuser. loginname, myuser. Email, myuser. Name, myuser. Notes );
// Bind the Set permissions
Myroleassignment. roledefinitionbindings. Add (Web. roledefinitions. getbytype (sproletype. Reader ));
// Add this permission to our list
Listitem. roleassignments. Add (myroleassignment );
Listitem. Update ();
Web. allowunsafeupdates = false;
Return true;
}
}
}
Catch
{
Return false;
}
}
// Obtain list items by ID
Public static string getroleassignmentsofsplistitem (string listname, int Itemid)
{
String revalue = string. empty;
Try
{
Using (spsite site = new spsite (siteurl ))
{
Using (spweb web = site. openweb ())
{
Web. allowunsafeupdates = true;
Splist list = web. Lists [listname];
Splistitem item = List. Items. getitembyid (Itemid );
Sproleassignmentcollection rolecoll = item. roleassignments;
Foreach (sproleassignment role in rolecoll)
{
For (INT I = 0; I <role. roledefinitionbindings. Count; I ++)
{
Revalue + = (role. member. loginname + ":" + role. roledefinitionbindings [I]. name + ":" + role. roledefinitionbindings [I]. basepermissions. tostring ());
}
}
Web. allowunsafeupdates = false;
}
}
}
Catch
{
}
Return revalue;
}