November 27, 2016

// // Leave a Comment

Create List and add content type using SharePoint PnP Framework in SharePoint online



  1. Install the nuget package to visual studio, if it is not installed
  2. Create a sample console application 
  3. Install the SharePointPnPCoreOnline dll, using the below nuget command.
  4. Add Environment variable as MSOPWD for the SP online account password
  5.  1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
          string pwd = System.Environment.GetEnvironmentVariable("EnvironmentVariableName", EnvironmentVariableTarget.User);
                 if(string.IsNullOrEmpty(pwd))
                 {
                     System.Console.WriteLine("MSOPWD user env variable is empty");
                     System.Console.ReadKey();
                     return;
                 }
                 using (var ctx = new ClientContext("https:YourOffice365Site"))
                 {
                     var password = new SecureString();
                     foreach (char c in pwd.ToCharArray()) password.AppendChar(c);
                     ctx.Credentials = new SharePointOnlineCredentials("YourEmailid", password);
     
                     ctx.Web.CreateContentType("CTbyPrem", "0x010100A2261C0C02164309A2B9FD587256ED56", "CTbyGp");
                     var list = ctx.Web.CreateList(ListTemplateType.DocumentLibrary, "ListbyPrem", false, enableContentTypes: true);
                     list.AddContentTypeToListByName("CTbyPrem", defaultContent: true);
                }
    

0 comments:

Post a Comment