Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Adding user as site collection administrator (for my site) via CSOM/REST

$
0
0

Hi, I'd like to know if it's possible to add current (global admin) user to a site collection as an administrator via CSOM or the REST API. Our background is that we're trying to access users' OneDrive for Business files via the API and I've understood that for that to work, the administrator in question has to be specifically added as site collection admin to user's MySite site collection. Yet when I try to do that with CSOM code:

var usernameSanitized = Regex.Replace(userId, "[.@]", "_");
var mySiteAddress = "https://mytenant-my.sharepoint.com/";
var userMySiteAddress = string.Format("{0}/personal/{1}/", mySiteAddress, usernameSanitized);
using (var context = new ClientContext(userMySiteAddress))
{
	context.AuthenticationMode = ClientAuthenticationMode.Default;
	var password = _appSettings.Password;
	SecureString secureString = StringToSecureString(password);
	context.Credentials = new SharePointOnlineCredentials(_appSettings.Username, secureString);
	context.ExecuteQuery();
	var user = context.Web.EnsureUser(_appSettings.Username);
	context.Load(user);
	context.ExecuteQuery();
	user.IsSiteAdmin = true;
	user.Update();
	user.Context.ExecuteQuery();
}

Everything else in the code works but the last ExecuteQuery() throws an Exception: "Microsoft.SharePoint.Client.ServerException: You need to be a site collection administrator to set this property."

Is this not supported via CSOM (from a standalone .net application)? 


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>