Set permission for user programmatically? (sharepoint)

sharepoint

I am using the following code to set permission for groups when I create a site:

            // Assign Site Owner role to the selected users
            string siteOwnerGroup = null;
            string siteOwnerRole = null;
            foreach (ListItem item in lbSiteOwner.Items)
            {
                siteOwnerGroup = item.Text.ToString();
                siteOwnerRole = "Full Control";

                SPRoleAssignment roleAssignment = new SPRoleAssignment(web.SiteGroups[siteOwnerGroup]);
                SPRoleDefinitionBindingCollection roleDefinition = roleAssignment.RoleDefinitionBindings;

                roleDefinition.Add(web.RoleDefinitions[siteOwnerRole]);
                web.RoleAssignments.Add(roleAssignment);
                web.Properties[siteOwnerGroup] = siteOwnerRole;
                web.Properties.Update();
            }

Shouldn't it be easy to change this to set permission for users in the lbSiteOwner listbox instead?

I tried

            SPRoleAssignment roleAssignment = new SPRoleAssignment(web.SiteUsers[siteOwnerGroup]);

But it doesn't work, any ideas?

Thanks in advance.

Best Answer

following solution will hold true in your case as well in place of list use web Programatically add user permission to a list in Sharepoint

Related Topic