SharePoint server-side object Model Operations document library (upload/grant/view permissions)

Source: Internet
Author: User
Tags foreach empty object model tostring

Introduction: Uploading Documents to a document library, and project-level authorization, viewing project-level permissions methods

      Create a folder under the list root public static string Creatfoldertospdoclib (String strfoldername, String strdoclibname) {string FolderPath = string.
  
  
  
          Empty; try {using (SPSite site = new SPSite (SiteURL)) {Usin G (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;
  
      The catch {} return folderpath; //Upload files to folder and authorize to relevant users public static bool Uploadfiletofolder (byte[] FileStream, string FileName, str ing 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 item so that it can set independent permissions listitem.breakroleinheritance (TRUE); Remove all previously inherited permissions from foreach (SPRoleAssignment roleassignment inlistitem.roleassignments) {RoleAssignment.RoleDefinitionBindings.Re
  
                          Moveall ();
  
                          Roleassignment.update ();
  
                      Listitem.update (); //Get the user SPUser MyUser = web that will set permissions.
  
                      Ensureuser (Allloginname);  Define permission Assignment SPRoleAssignment myroleassignment = new SPRoleAssignment (Myuser.loginname, Myuser.email,
  
                      Myuser.name, myuser.notes); The permissions set by the binding are MYROLEASSIGNMENT.ROLEDEFINITIONBINDINGS.ADD (web.
  
                      Roledefinitions.getbytype (Sproletype.reader));
  
                      Add this permission to our list of LISTITEM.ROLEASSIGNMENTS.ADD (myroleassignment);
  
  
  
                      Listitem.update (); Web.
  
                      Allowunsafeupdates = false;
  
                  return true;
  
            }  catch {return false; Get list item public static string Getroleassignmentsofsplistitem by ID (string listname, int ItemID) {String revalue = string.
  
          Empty; try {using (SPSite site = new SPSite (SiteURL)) {Usin G (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;
  
      The catch {} return revalue; }

PostScript: A few simple methods, test pass, may not be perfect, need words can continue to improve.

Author: cnblogs Lin Yu

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/web/sharepoint/

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.