Why do I get Auto-generated Permission Level 64b6593a-75eb-4031-ae23-0048de865fa3 instead of actual permission level name?

Problem

I am trying to create a new group and set a custom permission level(mask number is 196615) to that group with the following web services.

  • AddGroup using usergroup.asmx
  • AddPermission using permissions.asmx

Everything is working fine but the Permission Level name is updated with “Auto-generated Permission Level 64b6593a-75eb-4031-ae23-0048de865fa3” instead of the name I have given while creating the permission level.

I am using the below SOAP

AddPermission

<?xml version=”1.0″ encoding=”utf-16″?>

<soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”xmlns:xsd=”http://www.w3.org/2001/XMLSchema”&gt;

<soap:Body>

<AddPermissionxmlns=”http://schemas.microsoft.com/sharepoint/soap/directory/”&gt;

<objectName>Site54</objectName>

<objectType>Web</objectType>

<permissionIdentifier>Site54</permissionIdentifier>

<permissionType>group</permissionType>

<permissionMask>1011028719</permissionMask>

</AddPermission>

</soap:Body>

</soap:Envelope>

If  I assign this custom permission level to a new SharePoint group through SharePoint UI then I am getting the exact name what I gave for the custom permission level. I am facing this issue only when I try to set the permission through web service

I am getting the below XML when I ran the GetPermissionCollectionweb method. Here I am setting my custom permission level to the “Custom Joseph Group“ SharePoint group.

<soap:Envelope xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”xmlns:xsd=”http://www.w3.org/2001/XMLSchema”&gt;

<soap:Body>

<GetPermissionCollectionResponsexmlns=”http://schemas.microsoft.com/sharepoint/soap/directory/”&gt;

<GetPermissionCollectionResult>

<GetPermissionCollection>

<Permissions>

<Permission MemberID=”2″ Mask=”-1″MemberIsUser=”True” MemberGlobal=”False” UserLogin=”Domain\\joseph” />

<Permission MemberID=”4″ Mask=”138612801″MemberIsUser=”False” MemberGlobal=”True” GroupName=”Viewers” />

<Permission MemberID=”5″ Mask=”134287360″MemberIsUser=”True” MemberGlobal=”False” UserLogin=”NT AUTHORITY\authenticated users” />

<Permission MemberID=”6″ Mask=”-1″MemberIsUser=”False” MemberGlobal=”True” GroupName=”Joseph R&amp;D Works Owners” />

<Permission MemberID=”7″ Mask=”138612833″MemberIsUser=”False” MemberGlobal=”True” GroupName=”Joseph R&amp;D Works Visitors”/>

<Permission MemberID=”8″ Mask=”1006834407″MemberIsUser=”False” MemberGlobal=”True” GroupName=”Joseph R&amp;D Works Members”/>

<Permission MemberID=”10″ Mask=”134287360″MemberIsUser=”False” MemberGlobal=”True” GroupName=”Style Resource Readers” />

<Permission MemberID=”11″ Mask=”1012866047″MemberIsUser=”False” MemberGlobal=”True” GroupName=”Designers” />

<Permission MemberID=”12″ Mask=”2129075183″MemberIsUser=”False” MemberGlobal=”True” GroupName=”Hierarchy Managers” />

<Permission MemberID=”13″ Mask=”1011028991″MemberIsUser=”False” MemberGlobal=”True” GroupName=”Approvers” />

<Permission MemberID=”14″ Mask=”196641″MemberIsUser=”False” MemberGlobal=”True” GroupName=”Restricted Readers” />

<Permission MemberID=”54″ Mask=”134287360″MemberIsUser=”True” MemberGlobal=”False” UserLogin=”Domain\smarappan” />

<Permission MemberID=”94″ Mask=”196615″MemberIsUser=”False” MemberGlobal=”True” GroupName=”WIKI Site Owners” />

<Permission MemberID=”1073741823″Mask=”134287360″ MemberIsUser=”True” MemberGlobal=”False”UserLogin=”SHAREPOINT\system” />

</Permissions>

</GetPermissionCollection>

</GetPermissionCollectionResult>

</GetPermissionCollectionResponse>

</soap:Body>

</soap:Envelope>

Solution 

The following thread has similar issue about this

http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopmentlegacy/thread/e14dfed9-21c4-423d-872b-dc3c03ba8156

The conclusion of the above thread is to create custom web service to fix this issue.

Now I tried setting the role to the SharePoint group using object model APIs. Its working perfectly

Please find the sample code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace CustomPermissionService
{
    class Program
    {
        static void Main(string[] args)
        {

            AddCustomSitePermission("http://ServerName/sites/JRDW/Site54", "WIKI Site Owners", "group", "Custom Joseph");

            AddCustomListPermission("http://ServerName/sites/JRDW/Site54","Domain\\joseph","user", "Custom Joseph", "test");               

        }

        private static void AddCustomListPermission(string siteName, string userAccountOrGroupName, string entityName, string roleName, string listName)
        {

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                //check site exist or not
                SPSite site = new SPSite(siteName);
                SPWeb web = site.OpenWeb();
                //check list exist or not
                SPList list = web.Lists[listName];
                if (isGroupAlreadyExist(web, userAccountOrGroupName) == true)
                {
                    SPGroup group = site.RootWeb.SiteGroups[userAccountOrGroupName];
                    AddCustomPermissionToList(web, list, group, roleName);

                }
                else if (isUserAlreadyExist(web, userAccountOrGroupName) == true)
                {
                    SPUser user = site.RootWeb.SiteUsers[userAccountOrGroupName];
                    AddCustomPermissionToList(web, list, user, roleName);
                }
                else
                {
                    //throw exception.
                }
            });
        }

        private static void AddCustomSitePermission(string siteName, string userAccountOrGroupName, string entityName, string roleName)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                //check site exist or not
                SPSite site = new SPSite(siteName);
                SPWeb web = site.OpenWeb();

                if (isGroupAlreadyExist(web, userAccountOrGroupName) == true)
                {
                    SPGroup group = site.RootWeb.SiteGroups[userAccountOrGroupName];
                    AddCustomPermissionToSite(web, group, roleName);

                }
                else if (isUserAlreadyExist(web, userAccountOrGroupName) == true)
                {
                    SPUser user = site.RootWeb.SiteUsers[userAccountOrGroupName];
                    AddCustomPermissionToSite(web, user, roleName);
                }
                else
                {
                    //throw exception.
                }

            });
        }

        public static void AddCustomPermissionToSite(SPWeb web, SPPrincipal entity, string roleName)
        {
            SPRoleAssignment roleAssignment = new SPRoleAssignment(entity);
            SPRoleDefinition byType = web.ParentWeb.RoleDefinitions[roleName];
            roleAssignment.RoleDefinitionBindings.Add(byType);
            web.RoleAssignments.Add(roleAssignment);
        }

        public static void AddCustomPermissionToList(SPWeb web, SPList list, SPPrincipal entity, string roleName)
        {
            SPRoleAssignment roleAssignment = new SPRoleAssignment(entity);
            SPRoleDefinition byType = web.ParentWeb.RoleDefinitions[roleName];
            roleAssignment.RoleDefinitionBindings.Add(byType);
            list.RoleAssignments.Add(roleAssignment);

        }

        public static bool isGroupAlreadyExist(SPWeb web, string groupName)
        {
            bool isExist = false;

            try
            {
                SPGroup group = web.ParentWeb.SiteGroups[groupName];
                isExist = true;
            }
            catch (SPException)
            {
                isExist = false;
            }
            catch (Exception)
            {
                isExist = false;
            }
            return isExist;
        }

        public static bool isUserAlreadyExist(SPWeb web, string loginName)
        {
            bool isExist = false;

            try
            {
                SPUser user = web.ParentWeb.SiteUsers[loginName];
                isExist = true;
            }
            catch (SPException)
            {
                isExist = false;
            }
            catch (Exception)
            {
                isExist = false;
            }
            return isExist;
        }
    }
}
Advertisements

About Joseph Velliah
As a SharePoint Developer my professional interests tend to be technical and SharePoint focused. I run a blog at "SP RIDER" where you can expect to read HOW TOs and scenarios that I run into during my day to day job. I hope my posts will give back a little to the community that is helped me.

Comments are closed.

%d bloggers like this: