Murali Kris

Murali Kris

  • 1.4k
  • 253
  • 45.6k

Error while adding a user to site collection administrator

Sep 16 2017 5:04 AM
 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)