Advertisement
Guest User

Untitled

a guest
Jan 13th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. ClientContext objContext = new ClientContext("https://test.sharepoint.com/sites/AnkitTest/");
  2. var securePassword = new SecureString();
  3. var usernameqw = "ankitk@m";
  4. var pwdqq = "xyz";
  5. foreach (char c in pwdqq)
  6. {
  7. securePassword.AppendChar(c);
  8. }
  9. var onlineCredentials = new SharePointOnlineCredentials(usernameqw, securePassword);
  10. objContext.Credentials = onlineCredentials;
  11. Web web = objContext.Web;
  12. Microsoft.SharePoint.Client.GroupCollection groupColl = web.SiteGroups;
  13. GroupCreationInformation objCreateInfo = new GroupCreationInformation();
  14. objCreateInfo.Title = groupname;
  15. Microsoft.SharePoint.Client.Group objGroup = objContext.Web.SiteGroups.Add(objCreateInfo);
  16. RoleDefinition objDefination = objContext.Web.RoleDefinitions.GetByName("Full Control");
  17. RoleDefinitionBindingCollection objBindingColl = new RoleDefinitionBindingCollection(objContext);
  18. objBindingColl.Add(objDefination);
  19. objContext.Web.RoleAssignments.Add(objGroup, objBindingColl);
  20. objContext.ExecuteQuery();
  21. List ls = objContext.Web.Lists.GetByTitle("RequestForm");
  22. objContext.Load(ls);
  23. objContext.ExecuteQuery();
  24. AddPermission(objContext,ls, groupname, "Full Control");
  25. }
  26. }
  27. }
  28. }
  29. }
  30.  
  31.  
  32.  
  33.  
  34.  
  35. }
  36. public static void AddPermission(ClientContext cpx,List list, string groupName, string permissionName)
  37.  
  38. {
  39.  
  40. Web web = cpx.Web;
  41. Site site = cpx.Site;
  42.  
  43.  
  44.  
  45. Principal userGroup = FindUSerorSiteGroup(groupName,cpx,site, groupName);
  46.  
  47. RoleAssignment spRoleAssign = list.RoleAssignments.GetByPrincipal(userGroup);
  48.  
  49. RoleDefinition role = web.RoleDefinitions.GetByName("Contribute");
  50.  
  51. spRoleAssign.RoleDefinitionBindings.Add(role);
  52.  
  53. spRoleAssign.Update();
  54.  
  55. list.Update();
  56. cpx.Load(list);
  57. cpx.ExecuteQuery();//Excpetion occuring at this point
  58.  
  59.  
  60.  
  61. }
  62.  
  63. private static Principal FindUSerorSiteGroup(string groupname,ClientContext coj,Site site, string userOrGroup)
  64. {
  65. Principal myuser = null;
  66. Web web = coj.Web;
  67.  
  68. if (userOrGroup != null)
  69.  
  70. {
  71. Microsoft.SharePoint.Client.GroupCollection gs = coj.Web.SiteGroups;
  72. coj.Load(gs);
  73. coj.Load(site.RootWeb.SiteGroups);
  74. coj.ExecuteQuery();
  75. //might be a group
  76.  
  77. foreach (Microsoft.SharePoint.Client.Group g in site.RootWeb.SiteGroups)
  78.  
  79. {
  80.  
  81. if (g.Title.ToUpper() == userOrGroup.ToUpper())
  82.  
  83. {
  84. myuser = g;
  85. break;
  86.  
  87. }
  88.  
  89. }
  90.  
  91. }
  92.  
  93. return myuser;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement