SharePoint 2013 AppModel Basics

2 comments
Hi There,

I will show you how to connect to the sharepoint online site (office 365) with our own console application and will do some basic operations like Adding and Updating the Items to List with out using the Credentials. For this we are using app-only policy method.

Step1: Appregistration :
Navigate to _layouts/15/appregnew.aspx of the sitecollection(as we don't have specfic web application while working on Office 365 environment). Click on generate for both client Id and Client secret.
Then we will see some thing like this.


Click on Generate for client Id and Client Secret, then click on create now page will look this.


Keep this Client Id and Client Secret, we will these for getting the Tokens and realm for our app.

with this we have done with our app registration.

Step2: Now we need to grant permissions to the app principal. to achive this we have create a new provider hosted app in sharepoint, we will give the permissions that require for this app.

To do this simply navigate this Url _layouts/15/appinv.aspx of our site collection  then it will prompt the below screen give the App ID which we got in the previous step and then click on Lookup it will fetch all the details, check the details and and in the Permission request XML copy and paste these lines and click on create Button.

<AppPermissionRequests AllowAppOnlyPolicy="true">
    <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="Manage" />
</AppPermissionRequests>

If you observe my XML code I have set true for allowapponlypolicy. through this app only we are going to connect/update the share point online list(permission will be obtained from this Provider hosted app).


With this we have done with our configurations now lets jump into our coding part. 

For this example we are  Nugetconsole for generating the Token please find the below steps to generate the requied classes for this application.




  • From Visual Studio- navigate to Tools -> Nuget Package Manager  -> and select  “Manage Nuget Packages for Solution” as shown in the below.

  • Select “Nuget.org” and search for “App for SharePoint Web Toolkit” and click on “Install”. This will add “TokenHelper.cs” and “SharePointContext.cs” classes to the solution. TokenHelper and SharePointContext classes help the console application in getting access to SharePoint site using client id and client secret.





  • Now open program.cs and paste the below code


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.SharePoint.Client;
    using System.Configuration;
    using System.Collections.Specialized;
    using System.IO;
    using System.Security;

    namespace ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
             
                    Uri siteUri = new Uri("https://{{yourcompanyname}}.sharepoint.com");
                    string listname = "Employee";
                    string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);
                    string accessToken = TokenHelper.GetAppOnlyAccessToken(
                        TokenHelper.SharePointPrincipal,
                        siteUri.Authority, realm).AccessToken;

                    using (var clientContext =
                        TokenHelper.GetClientContextWithAccessToken(
                            siteUri.ToString(), accessToken))
                    {
                     
                        AddListItem(clientContext, listname);
                    }
             
            }  
         
            private static void AddListItem(ClientContext clientContext, string listName)
            {
                Web currentWeb = clientContext.Web;
                var myList = clientContext.Web.Lists.GetByTitle(listName);
                ListItemCreationInformation listItemCreate = new ListItemCreationInformation();
                Microsoft.SharePoint.Client.ListItem newItem = myList.AddItem(listItemCreate);
                newItem["Title"] = "Item added by App";
                newItem["EmployeeName"] = "Vikranth Reddy";
                newItem.Update();
                clientContext.ExecuteQuery();
            }      
        }
    }
    Also please make sure that your app.config must have these values in appsettings tag


     <appSettings>
        <add key="ClientId" value="value we got from the step1"/>
        <add key="ClientSecret" value="value we got from the step1"/>
      </appSettings>


    Then we are ready go, hit f5 button then see the magic , our code will add an item in the Employee list of my site collection.

    That's all , thanks for reading the post.
    Post the comments if any issues in this solution.


    Thanks,
    Vikranth CH.

    Related Post

    2 comments: