SharePoint authorization to documents

Source: Internet
Author: User

// 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;

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.