1

I'm having some troubles with getting SPPrincipals, was hoping if anybody would point me to the right direction.

Here is the scoop: I need to grant permissions to SPItem. I get SPUser:

SPUser user = web.AllUsers.GetByID    // {i:0#.w|domain\ad_name}

Check what current permissions for the user:

SPRoleAssignment currentUserRole = item.RoleAssignments.GetAssignmentByPrincipal(user);

Getting exception:

{"Can not find the principal with id: 38."} // 38 is correct site user id

If I'm trying to resolve SPPrincipal:

SPPrincipalInfo pi = SPUtility.ResolvePrincipal(webApp, null, user.LoginName, SPPrincipalType.All, SPPrincipalSource.All, false);

Getting not null SPPrincipalInfo object but with PrincipleId = -1, not sure what it means.

Tried to google on "Can not find the principal with id:", could not find much. How come it correctly resolves site user id but does not recognize it as SPPrincipal id?

4

GetAssignmentByPrincipal throwing ArgumentException "Can not find the principal with id: {0}" says SPRoleAssignmentCollection doesn't contain a SPRoleAssignment for that principal. Basically, it's just a search from Principal-to-Roles hashtable, and it fails.

Something like foreach (var ra in item.RoleAssignments){Console.WriteLine(ra.Member.ID)} will list all suitable IDs that won't make it throw an exception.

Maybe you should check SPListItem.DoesUserHavePermissions and SPListItem.GetUserEffectivePermissions methods

  • Thanks Aziz! Does it mean just catch it and ignore? Will try next week – Victor Shostak Feb 27 '16 at 17:51
  • Sure, my pleasure. Please see the answer updated. – Aziz Kabyshev Feb 27 '16 at 17:59
  • Indeed, trapping the exception and ignoring it does the job. I was puzzled by the fact of exception, was not sure how to deal with it. Thanks Again! – Victor Shostak Mar 2 '16 at 19:12
0

Please see the following code example, when used, you can pass a site and login id, and get respective permissions, whether as a user or member in a group in the respective site.

public static string FindPermissionLevelForUser(string loginname, string strSite)

    {
        string strValue = string.Empty;
        try
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(strSite))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        string spreturnUser = string.Empty;

                        try
                        {
                            SPUser user = web.Users[loginname];
                            SPRoleAssignment currentUserRole = web.RoleAssignments.GetAssignmentByPrincipal(user);


                            foreach (SPRoleDefinition role in currentUserRole.RoleDefinitionBindings)
                            {
                                strValue += role.Name + ", ";
                            }
                        }
                        catch (Exception)
                        {
                            //if user in group
                            SPGroupCollection groupColl = web.Groups;

                            foreach (SPGroup group in groupColl)
                            {
                                try
                                {
                                    SPUser user = group.Users[loginname];
                                    SPRoleAssignment currentUserRole = web.RoleAssignments.GetAssignmentByPrincipal(group);

                                    foreach (SPRoleDefinition role in currentUserRole.RoleDefinitionBindings)
                                    {
                                        strValue += role.Name + ", ";
                                    }
                                }
                                catch (Exception)
                                {

                                }
                            }

                        }
                    }
                }
            });
        }
        catch (Exception ex)
        {

            return string.Empty;

        }
        return strValue;
    }
0

I would use the SPWeb.EnsureUser(loginName) to make sure the user is known to the item's parent web. This function will create a user reference / object in that SPWeb if the user is not already known for that specific SPWeb without granting actual permissions. The function return an SPUser object which you can then use to create an SPRoleAssignment with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.