1

Being very new to SharePoint, I've been trying to write a very basic app to create a new site inside my SharePoint Online tenancy, but I can't seem to get it authenticated. I'm using the CSOM objects for both the authentication & the creating of the site. I've also tried this same code in a console application, in case there was something wrong with my autohosted app, but I get the same results there.

My code is as follows:

var context = new ClientContext("https://mysptenancy.sharepoint.com/sites/btl");
var pwd = "Myp@ssw0rd";
var password = new SecureString();

foreach (var c in pwd.ToCharArray()) {
    password.AppendChar(c);
}

context.Credentials = new SharePointOnlineCredentials("me@mydomain.com", password);
context.ExecuteQuery(); // 401 occurs at this call

var createInfo = new WebCreationInformation {
    Title = "Console App Test",
    Url = "consoleapptest",
    WebTemplate = "My Template"
};

context.Web.Webs.Add(createInfo);

context.ExecuteQuery();

I've seen a lot of examples that don't contain the authentication code. If I take it out, I get a 403 result from the last line. Also, all the examples I've seen, has the username as one of the onmicrosoft accounts. Is this a requirement for authenticating against SP Online?

In terms of network configuration, we have a local AD server on our network, which syncs with Office 365. Because of this, my username is off of our domain, not onmicrosoft.

Any assistance would be greatly appreciated.

EDIT

We've now identified that it has something to do with the site permissions inside SharePoint it seems. When I target our Development Center site with my test app, I can create the new site just fine. But when I target our testing site (which was set up using the Team Site Template), I get the 401 error. We have confirmed that the account I'm using is listed as a Site Administrator for both.

But I'm still unsure what permission I would be missing, seeing as how I'm an administrator already for both sites. Has anyone run into this before?

  • Have you tried using the TokenHelper class to create the ClientContext instead of creating one yourself? – wjervis Apr 23 '14 at 13:22
  • I'm having some difficulty creating the SP App VS template on my home machine, but will try this in the morning when I'm back at the office, thanks for the idea. But how would you do this in something other than a SP App then? Would you copy the TokenHelper class file from an App project into, say, a console application? – Carl Heinrich Hancke Apr 23 '14 at 17:48
  • You can use what you've posted in a console app (or even PowerShell!). I think in order for the app to work on your test site, you have to deploy it to your catalog, then install on your site. You cannot debug that way, however. – wjervis Apr 23 '14 at 20:01
  • What permissions does your app have? Since you're editing a site that is not the site the app is being ran on, you need to use either site collection (if the sites are in the same collection), or tenant (if the sites are in different site collections). I know full control will allow the creation of a site, but manager or write may suffice. Also, if you plan to make changes to tenant objects (e.g., creating a site collection), or sites outside the current context, the app will need to be able to make app only calls. – wjervis Apr 24 '14 at 11:54
  • Sorry for such a late reply. We decided against this type of implementation, but I did run into a similar problem in another app I was working on. The way I got around it there was to use the TokenHelper class, as you suggested & creating a context to the other site using the same credentials of the currently logged in user. This fixed the 401 I was getting. Thanks for pointing me in the right direction! – Carl Heinrich Hancke Jun 5 '14 at 7:49

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.