I am Unable to Assign group and permissions to file or list

May 10 2019 3:36 AM
everytime exception occures "Cannot add a role assignment with empty role definition binding collection " , I tried all the solution from other sources still facing same issue..please help.. my code is as below
  1. internal bool AssignPermission(ClientContext p_Context ,SPList MicrosoftDestinationList, Folder DestinationFolder,File DestinationFile,ConcurrentBag<SPRoleDefinations> p_Permissions )  
  2. {  
  3. string f_UserName = null;  
  4. string f_GrpName = null;  
  5. bool f_Flag = false;  
  6. Principal f_Principal = null;  
  7. RoleDefinition f_RoleDef = null;  
  8. RDBCollection roleDefBindCol = null;  
  9. RACollection ItemRoleAssignCollection = null;  
  10. List<RoleDefinition> SiteRoleDefinationCollection = null;  
  11. List<string> UserList = null;  
  12. bool f_SameContext = false;  
  13. try  
  14. {  
  15. #region CHECK CLIENT CONTEXT  
  16. //CSOM need objects from same client context  
  17. //Check if client context of the Group/Users is same as the  
  18. //current client context. If both are same user current client  
  19. //context else get User/Groups again.  
  20. if (m_WebGroups.Count > 0)  
  21. {  
  22. if (m_WebGroups.ElementAt(0).Context.TraceCorrelationId ==p_Context.TraceCorrelationId)  
  23. f_SameContext = true;  
  24. }  
  25. else if (m_WebUsers.Count > 0)  
  26. {  
  27. if (m_WebUsers.ElementAt(0).Context.TraceCorrelationId ==p_Context.TraceCorrelationId)  
  28. f_SameContext = true;  
  29. }  
  30. if(!f_SameContext)  
  31. UserList = GetUsers(p_Context);  
  32. #endregion  
  33. try  
  34. {  
  35. if (p_Context.HasPendingRequest)  
  36. p_Context.ExecuteQuery();  
  37. }  
  38. catch (Exception){}  
  39. if(MicrosoftDestinationList!=null)  
  40. ItemRoleAssignCollection = MicrosoftDestinationList.RoleAssignments;  
  41. if (DestinationFile != null)  
  42. ItemRoleAssignCollection = DestinationFile.ListItemAllFields.RoleAssignments;  
  43. else  
  44. if (DestinationFolder != null)  
  45. ItemRoleAssignCollection = DestinationFolder.ListItemAllFields.RoleAssignments;  
  46. //p_Context.Load(p_SpDestList.RoleAssignments);  
  47. //p_Context.ExecuteQuery();  
  48. SiteRoleDefinationCollection = m_WebRoleDefinations[p_Context.Web.Url];  
  49. foreach (SPRoleDefinations f_RoleDefObj in p_Permissions)  
  50. {  
  51. try  
  52. {  
  53. bool f_RoleAdeed = false;  
  54. /// m_WebRoleDefinations contains site and its Role Definations  
  55. roleDefBindCol = new RDBCollection(p_Context);  
  56. foreach (SPROle f_Role in f_RoleDefObj.m_Roles)  
  57. {  
  58. try  
  59. {  
  60. // Limited access role is provided by default  
  61. if (f_Role.m_Name == "Limited Access")  
  62. continue;  
  63. RoleType f_RoleType = RoleType.None;  
  64. ///Check if rolekind is present for the website  
  65. if (Enum.TryParse(f_Role.m_RoleKind, out f_RoleType))  
  66. {  
  67. if (null != SiteRoleDefinationCollection.Where(rd => rd.RoleTypeKind == f_RoleType).FirstOrDefault())  
  68. {  
  69. var roledef = p_Context.Web.RoleDefinitions.GetByType(f_RoleType);  
  70. p_Context.Load(roledef);  
  71. p_Context.ExecuteQuery();  
  72. //roleDefBindCol.Add(p_Context.Web.RoleDefinitions.  
  73. // GetByType(f_RoleType));  
  74. roleDefBindCol.Add(roledef);  
  75. f_RoleAdeed = true;  
  76. }  
  77. else  
  78. {  
  79. /// If role is not found create new role and add to website permissions  
  80. f_RoleDef = CreateNewRoleDefination(p_Context, f_Role);  
  81. if(f_RoleDef!=null)  
  82. roleDefBindCol.Add(f_RoleDef);  
  83. f_RoleAdeed = true;  
  84. }  
  85. if(MicrosoftDestinationList!=null)  
  86. MicrosoftDestinationList.BreakRoleInheritance(truefalse);  
  87. if (DestinationFile != null)  
  88. DestinationFile.ListItemAllFields.BreakRoleInheritance(truefalse);  
  89. else  
  90. if (DestinationFolder != null)  
  91. DestinationFolder.ListItemAllFields.BreakRoleInheritance(truefalse);  
  92. p_Context.ExecuteQuery();  
  93. }  
  94. }  
  95. catch (Exception ex)  
  96. { CommonClass.ExLogger.LogError(ClassName, "AssignPermission:: Log 1 :: ", ex); }  
  97. }  
  98. if (!f_RoleAdeed)  
  99. continue;  
  100. /// Get user or group and add to RoleAssignmentCollection  
  101. f_Principal = null;  
  102. if (!f_RoleDefObj.m_Isgroup)  
  103. {  
  104. continue;  
  105. //f_UserName = f_RoleDefObj.m_Members.Select(member =>  
  106. // member.m_EmailID).First();  
  107. //f_UserName = CommonClass.UserMappings[f_UserName];  
  108. //f_Principal = m_WebUsers.Where(usr =>  
  109. // ((User)usr).Email.ToLower() ==  
  110. // f_UserName.ToLower()).First();  
  111. }  
  112. else  
  113. {  
  114. if (CommonClass.UserMappings.Keys.Contains(f_RoleDefObj.m_SpGroupName))  
  115. {  
  116. f_GrpName = CommonClass.UserMappings[f_RoleDefObj.m_SpGroupName];  
  117. f_Principal = m_WebGroups.Where(grp =>grp.Title.ToLower() ==f_GrpName.ToLower()).First();  
  118. p_Context.Load(f_Principal);  
  119. p_Context.ExecuteQuery();  
  120. //SecurableObject securable = p_Context.Web.Lists.GetByTitle(DestinationFolder.Name).GetItemByUniqueId(DestinationFile.UniqueId);  
  121. //securable.BreakRoleInheritance(true, false);  
  122. //f_roleAssignCollection = DestinationFile.ListItemAllFields.RoleAssignments;  
  123. //RoleAssignment rollassign= f_roleAssignCollection.Add(f_Principal, roleDefBindCol);  
  124. ItemRoleAssignCollection.Add(f_Principal, roleDefBindCol);  
  125. p_Context.Load(ItemRoleAssignCollection);  
  126. if (MicrosoftDestinationList != null)  
  127. MicrosoftDestinationList.Update();  
  128. if (DestinationFile != null)  
  129. DestinationFile.Update();  
  130. else  
  131. if (DestinationFolder != null)  
  132. DestinationFolder.Update();  
  133. p_Context.ExecuteQuery();  
  134. f_Flag = true;  
  135. }  
  136. }  
  137. //f_roleAssignCollection.Add(f_Principal, roleDefBindCol);  
  138. }  
  139. catch (Exception ex)  
  140. { CommonClass.ExLogger.LogError(ClassName, "AssignPermission:: Log 2 :: ", ex); }  
  141. }  
  142. //p_Context.Load(f_roleAssignCollection);  
  143. //p_Context.ExecuteQuery();  
  144. }  
  145. catch (Exception ex)  
  146. { CommonClass.ExLogger.LogError(ClassName, "AssignPermission", ex); }  
  147. return f_Flag;  
  148. }  

Answers (2)