1
Answer

Error while adding a user to site collection administrator

Murali Kris

Murali Kris

7y
258
1
 Hi All,
please help me from below issue.
 
get-access is button it reads input from text box , and it has to add that user id in to as a site collcetion administrator.
  
 
please see below code. where i am getting error.
 
private void button10_Click(object sender, EventArgs e)
{
string adduser = string.Empty;// textBox4.Text.Trim();
adduser = textBox4.Text.Trim();
if (Context == null)
{
Context = Get_Context();
}
Context.Load(Context.Site);
Context.Load(Context.Site.RootWeb);
Context.Load(Context.Site.RootWeb.SiteUsers);
Context.ExecuteQuery();
foreach (var scUser in Context.Site.RootWeb.SiteUsers)
{
if (scUser.IsSiteAdmin)
{
//here i am getting error 
User user = Web.EnsureUser(adduser);
// another formate tryed below also getting error same .
User user = Web.EnsureUser(@"domain\username").ToString(); 
 
Error : an Object referance is required for the non-static field, method, or property 'web.ensureUser(string)'
// Context.Site.RootWeb.SiteUsers.AddUser()
// Context.Site.RootWeb.SiteUsers.AddUser(scUser.Id);
//Context.Site.RootWeb.SiteUsers.AddUser(user);
Context.Site.RootWeb.SiteUsers.AddUser(user);
}
}
 
please can any one help me here. 
 
 
Answers (1)