TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Bhagyashri Sonar
NA
37
3.2k
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
internal
bool
AssignPermission(ClientContext p_Context ,SPList MicrosoftDestinationList, Folder DestinationFolder,File DestinationFile,ConcurrentBag<SPRoleDefinations> p_Permissions )
{
string
f_UserName =
null
;
string
f_GrpName =
null
;
bool
f_Flag =
false
;
Principal f_Principal =
null
;
RoleDefinition f_RoleDef =
null
;
RDBCollection roleDefBindCol =
null
;
RACollection ItemRoleAssignCollection =
null
;
List<RoleDefinition> SiteRoleDefinationCollection =
null
;
List<
string
> UserList =
null
;
bool
f_SameContext =
false
;
try
{
#region CHECK CLIENT CONTEXT
//CSOM need objects from same client context
//Check if client context of the Group/Users is same as the
//current client context. If both are same user current client
//context else get User/Groups again.
if
(m_WebGroups.Count > 0)
{
if
(m_WebGroups.ElementAt(0).Context.TraceCorrelationId ==p_Context.TraceCorrelationId)
f_SameContext =
true
;
}
else
if
(m_WebUsers.Count > 0)
{
if
(m_WebUsers.ElementAt(0).Context.TraceCorrelationId ==p_Context.TraceCorrelationId)
f_SameContext =
true
;
}
if
(!f_SameContext)
UserList = GetUsers(p_Context);
#endregion
try
{
if
(p_Context.HasPendingRequest)
p_Context.ExecuteQuery();
}
catch
(Exception){}
if
(MicrosoftDestinationList!=
null
)
ItemRoleAssignCollection = MicrosoftDestinationList.RoleAssignments;
if
(DestinationFile !=
null
)
ItemRoleAssignCollection = DestinationFile.ListItemAllFields.RoleAssignments;
else
if
(DestinationFolder !=
null
)
ItemRoleAssignCollection = DestinationFolder.ListItemAllFields.RoleAssignments;
//p_Context.Load(p_SpDestList.RoleAssignments);
//p_Context.ExecuteQuery();
SiteRoleDefinationCollection = m_WebRoleDefinations[p_Context.Web.Url];
foreach
(SPRoleDefinations f_RoleDefObj
in
p_Permissions)
{
try
{
bool
f_RoleAdeed =
false
;
/// m_WebRoleDefinations contains site and its Role Definations
roleDefBindCol =
new
RDBCollection(p_Context);
foreach
(SPROle f_Role
in
f_RoleDefObj.m_Roles)
{
try
{
// Limited access role is provided by default
if
(f_Role.m_Name ==
"Limited Access"
)
continue
;
RoleType f_RoleType = RoleType.None;
///Check if rolekind is present for the website
if
(Enum.TryParse(f_Role.m_RoleKind,
out
f_RoleType))
{
if
(
null
!= SiteRoleDefinationCollection.Where(rd => rd.RoleTypeKind == f_RoleType).FirstOrDefault())
{
var roledef = p_Context.Web.RoleDefinitions.GetByType(f_RoleType);
p_Context.Load(roledef);
p_Context.ExecuteQuery();
//roleDefBindCol.Add(p_Context.Web.RoleDefinitions.
// GetByType(f_RoleType));
roleDefBindCol.Add(roledef);
f_RoleAdeed =
true
;
}
else
{
/// If role is not found create new role and add to website permissions
f_RoleDef = CreateNewRoleDefination(p_Context, f_Role);
if
(f_RoleDef!=
null
)
roleDefBindCol.Add(f_RoleDef);
f_RoleAdeed =
true
;
}
if
(MicrosoftDestinationList!=
null
)
MicrosoftDestinationList.BreakRoleInheritance(
true
,
false
);
if
(DestinationFile !=
null
)
DestinationFile.ListItemAllFields.BreakRoleInheritance(
true
,
false
);
else
if
(DestinationFolder !=
null
)
DestinationFolder.ListItemAllFields.BreakRoleInheritance(
true
,
false
);
p_Context.ExecuteQuery();
}
}
catch
(Exception ex)
{ CommonClass.ExLogger.LogError(ClassName,
"AssignPermission:: Log 1 :: "
, ex); }
}
if
(!f_RoleAdeed)
continue
;
/// Get user or group and add to RoleAssignmentCollection
f_Principal =
null
;
if
(!f_RoleDefObj.m_Isgroup)
{
continue
;
//f_UserName = f_RoleDefObj.m_Members.Select(member =>
// member.m_EmailID).First();
//f_UserName = CommonClass.UserMappings[f_UserName];
//f_Principal = m_WebUsers.Where(usr =>
// ((User)usr).Email.ToLower() ==
// f_UserName.ToLower()).First();
}
else
{
if
(CommonClass.UserMappings.Keys.Contains(f_RoleDefObj.m_SpGroupName))
{
f_GrpName = CommonClass.UserMappings[f_RoleDefObj.m_SpGroupName];
f_Principal = m_WebGroups.Where(grp =>grp.Title.ToLower() ==f_GrpName.ToLower()).First();
p_Context.Load(f_Principal);
p_Context.ExecuteQuery();
//SecurableObject securable = p_Context.Web.Lists.GetByTitle(DestinationFolder.Name).GetItemByUniqueId(DestinationFile.UniqueId);
//securable.BreakRoleInheritance(true, false);
//f_roleAssignCollection = DestinationFile.ListItemAllFields.RoleAssignments;
//RoleAssignment rollassign= f_roleAssignCollection.Add(f_Principal, roleDefBindCol);
ItemRoleAssignCollection.Add(f_Principal, roleDefBindCol);
p_Context.Load(ItemRoleAssignCollection);
if
(MicrosoftDestinationList !=
null
)
MicrosoftDestinationList.Update();
if
(DestinationFile !=
null
)
DestinationFile.Update();
else
if
(DestinationFolder !=
null
)
DestinationFolder.Update();
p_Context.ExecuteQuery();
f_Flag =
true
;
}
}
//f_roleAssignCollection.Add(f_Principal, roleDefBindCol);
}
catch
(Exception ex)
{ CommonClass.ExLogger.LogError(ClassName,
"AssignPermission:: Log 2 :: "
, ex); }
}
//p_Context.Load(f_roleAssignCollection);
//p_Context.ExecuteQuery();
}
catch
(Exception ex)
{ CommonClass.ExLogger.LogError(ClassName,
"AssignPermission"
, ex); }
return
f_Flag;
}
Reply
Answers (
2
)
SharePoint Provisioning did not succeed
Haw to show user detail(job title)from people picker