1

In Module 2 of the MVA course Building Blocks and Services of the SharePoint Platform titled ‘Deep Dive into SharePoint Lists for Data Storage’ (at about 45 minutes in) Ted Pattison did a demo of using a console app to create a list on a SharePoint Online site. The course is at http://www.microsoftvirtualacademy.com/training-courses/deep-dive-building-blocks-and-services-of-sharepoint

I’m trying to do the same in my environment but I’m having trouble.

In the demo he went to _layouts/15/AppRegNew.aspx to register a new app in the app registry. In the demo, at the top of the page there was an ‘App Type’ radio Button list with the options ‘An app running on the web Server’ and ‘An App running on a client machine’. When I access this page on my site, there is no such radio Button List. Also in the demo, Ted left the Redirect URL blank. On my site it is required: enter image description here So to get past this I entered the URL for my site (https://mydomain.sharepoint.com/sites/test). The app ID was created successfully: enter image description here

I then went to _layouts/15/AppInv.aspx to give the app security. I pasted in the CAML to give the app read access to the web:

  <AppPermissionRequests>
    <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="Read" />
  </AppPermissionRequests>

enter image description here And then trusted the app by Clicking Trust It:

enter image description here

I then copied the values from the App registration into my app.config:

 <add key="targetSiteUrl" value="https://xxxxx.sharepoint.com/sites/test"/>
    <add key="clientId" value="bf4c37ef-9202-41ba-8430-3983cba26285"/>
    <add key="clientSecret" value="nKGefHSvT69Ls2rwq1AIVyyHkIwlBzT9UkpbJMUcIbw="/>
    <add key="deleteOnly" value="false"/>

And then created code based on what was in the demo to get the webs title:

static void Main(string[] args)
        {
            string siteUrl = ConfigurationManager.AppSettings["targetSiteUrl"];
            bool deleteOnly = ConfigurationManager.AppSettings["deleteOnly"].Equals("true");


                Uri siteUri = new Uri(siteUrl);
            string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);
            var accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal,
                siteUri.Authority, realm).AccessToken;
            using (var clientContext = TokenHelper.GetClientContextWithAccessToken(siteUrl, accessToken)) {

                var web = clientContext.Web;
                clientContext.Load(web);
                clientContext.ExecuteQuery();

                Console.WriteLine(web.Title);

            }



        }

The code above gets the realm and the access token and successfully creates the clientContext, But when I run the executeQuery I always get the error Microsoft.SharePoint.Client.ServerUnauthorizedAccessException. I have tried giving the app ID full control of the Web, the Site Collection and the Tenant, but I still get the same error.

How do I get my console app to have update access to my site?

RussGove
  • 322
  • 5
  • 18
  • I got it working using the code at http://www.vrdmn.com/2013/01/authenticating-net-client-object-model.html But I am still curious how to get it working using an app id. – RussGove Feb 25 '15 at 19:12

1 Answers1

1

I needed to set AllowAppOnlyPolicy when adding the permissions in appinv.aspx

<AppPermissionRequests AllowAppOnlyPolicy="true" >
RussGove
  • 322
  • 5
  • 18