0

I tried the below code for fetching the Groups from 2007 SharePoint Site and adding it in 2013 site. Now I am also want to assign unique permission to the groups in the SharePoint List as it is in 2007. From the below code I get the exception as Cannot find the principal below ID. Kindly enlighten what am I doing wrong.Thanks

private void CopyPermissions() { XYZ_Migration_Tool.WS_Permissions.Permissions ps = new WS_Permissions.Permissions(); ps.PreAuthenticate = true; ps.Credentials = new NetworkCredential("xyz", "xyz"); XmlNode nps = ps.GetPermissionCollection("Announcements", "List"); foreach(XmlNode ngs in nps) { string groupname = string.Empty; if(ngs.Name== "Permissions") { var percount = ngs.ChildNodes.Count; for (int pi= 0; pi

                        ClientContext objContext = new ClientContext("https://test.sharepoint.com/sites/AnkitTest/");
                        var securePassword = new SecureString();
                        var usernameqw = "ankitk@m";
                        var pwdqq = "xyz";
                        foreach (char c in pwdqq)
                        {
                            securePassword.AppendChar(c);
                        }
                        var onlineCredentials = new SharePointOnlineCredentials(usernameqw, securePassword);
                        objContext.Credentials = onlineCredentials;
                        Web web = objContext.Web;
                        Microsoft.SharePoint.Client.GroupCollection groupColl = web.SiteGroups;
                        GroupCreationInformation objCreateInfo = new GroupCreationInformation();
                        objCreateInfo.Title = groupname;
                        Microsoft.SharePoint.Client.Group objGroup = objContext.Web.SiteGroups.Add(objCreateInfo);
                        RoleDefinition objDefination = objContext.Web.RoleDefinitions.GetByName("Full Control");
                        RoleDefinitionBindingCollection objBindingColl = new RoleDefinitionBindingCollection(objContext);
                        objBindingColl.Add(objDefination);
                        objContext.Web.RoleAssignments.Add(objGroup, objBindingColl);
                        objContext.ExecuteQuery();
                        List ls = objContext.Web.Lists.GetByTitle("RequestForm");
                        objContext.Load(ls);
                        objContext.ExecuteQuery();
                        AddPermission(objContext,ls, groupname, "Full Control");
                    }
                }
            }
        }
    }





}
public static void AddPermission(ClientContext cpx,List list, string groupName, string permissionName)

{

    Web web = cpx.Web;
    Site site = cpx.Site;



    Principal userGroup = FindUSerorSiteGroup(groupName,cpx,site, groupName);

        RoleAssignment spRoleAssign = list.RoleAssignments.GetByPrincipal(userGroup);

        RoleDefinition role = web.RoleDefinitions.GetByName("Contribute");

        spRoleAssign.RoleDefinitionBindings.Add(role);

        spRoleAssign.Update();

        list.Update();
    cpx.Load(list);
    cpx.ExecuteQuery();//Excpetion occuring at this point



}

private static Principal FindUSerorSiteGroup(string groupname,ClientContext coj,Site site, string userOrGroup)
{
    Principal myuser = null;
    Web web = coj.Web;

    if (userOrGroup != null)

                    {
        Microsoft.SharePoint.Client.GroupCollection gs = coj.Web.SiteGroups;
        coj.Load(gs);
        coj.Load(site.RootWeb.SiteGroups);
        coj.ExecuteQuery();
        //might be a group

        foreach (Microsoft.SharePoint.Client.Group g in site.RootWeb.SiteGroups)

        {

            if (g.Title.ToUpper() == userOrGroup.ToUpper())

            {
                myuser = g;
                break;

            }

        }

    }

    return myuser;
}
  • You need to clean up this post and let ppl know where the code is failing. – jpollar Jan 13 '16 at 20:58
  • I have mentioned the line in comments where I am getting the exception. code is failing in cpx.ExecuteQuery() in AddPermission() method. The exception thrown is Cannot find the principal below ID. – Ankit Kumar Jan 14 '16 at 4:02

Your Answer

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

Browse other questions tagged or ask your own question.