How to create a site-collection using CSOM in SharePoint Online

In this post, we’ll see how to create a site-collection using SharePoint 2013 CSOM in SharePoint Online. We’ll be using a console application for the purpose of demonstration.

Open Visual Studio 2013 –> File –> New –> Console Application and name it as ‘CreateSiteCollCSOM’.

Add references to the following assemblies

Microsoft.SharePoint.Client.dll

Microsoft.SharePoint.Client.runtime.dll

Microsoft.Online.SharePoint.Client.Tenant.dll

.

The Microsoft.Online.SharePoint.Client.Tenant.dll will be available in the location C:Program FilesSharePoint Client ComponentsAssemblies, if the SharePoint Client Components SDK is installed.

Import the following namespaces at the top of Program.cs

using  Microsoft.SharePoint.Client;
using System.Security;
using Microsoft.Online.SharePoint.TenantAdministration;

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

static void Main(string[] args)
        {
            


            using (ClientContext oClientContext = new ClientContext("https://yoursite-admin.sharepoint.com"))
            {
                //Assign User Id for your SharePoint Online tenant     
                string UserName = "userid@yoursite.onmicrosoft.com";

                //Assign password for your SharePoint online tenant
                string Password = "password";

                //Create a SecureString object from password string, needed for SharePointOnlineCredentials class
                SecureString SecurePassword = GetSecureString(Password);

                oClientContext.Credentials = new SharePointOnlineCredentials(UserName, SecurePassword);
                

                var oTenant = new Tenant(oClientContext);

                var oSiteCreationProperties = new SiteCreationProperties();


                //Set the url of site-collection to be created
                oSiteCreationProperties.Url = "https://yoursite.sharepoint.com/sites/TestSiteColl";

                //Set the title of site
                oSiteCreationProperties.Title = "Test SiteColl from code";

                //set the site-collection owner
                oSiteCreationProperties.Owner = "userid@yoursite.onmicrosoft.com";

                //set the template of site-collection to be created as TeamSite
                oSiteCreationProperties.Template = "STS#0";

                //set the storge maxium level in MB
                oSiteCreationProperties.StorageMaximumLevel = 200;

                oSiteCreationProperties.UserCodeMaximumLevel = 100;



                SpoOperation oSpoOperation = oTenant.CreateSite(oSiteCreationProperties);

                oClientContext.Load(oTenant);

                oClientContext.Load(oSpoOperation, i=>i.IsComplete);
                oClientContext.ExecuteQuery();

               

                Console.WriteLine("SiteCollection successfully reated");




            }




        }

        private static SecureString GetSecureString(String Password)
        {
            SecureString oSecurePassword = new SecureString();

            foreach (Char c in Password.ToCharArray())
            {
                oSecurePassword.AppendChar(c);

            }
            return oSecurePassword;
        }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Azure WebJobs – Q&A’s

Here are some important Q&A’s around AzureWebjobs.

1. What is WebJobs in Azure?

The WebJobs in Azure enables us to run programs or scripts in Azure web sites in one of the following 3 ways :-

  • On Demand
  • Continuously
  • Scheduled

 

2. What are the acceptable file types for Scripts to create WebJobs?

It supports the set of file types creating using the following technologies:-

  • using windows cmd (.cmd, .bat and .exe)
  • using powershell (.ps1)
  • using bash (.sh)
  • using php (.php)
  • using python (.py)
  • using node (.js)

3. Where are WebJobs deployed?

WebJobs are deployed as a part of web site or ftp-ed into a specific directory. Once the WebJobs is deployed to the Azure web site, it gives an extra dashboard which details out the job execution history.

4. How to set up a WebJobs?

First you need to create an Azure web site for setting up the WebJobs. Click WebJobs (preview) at the top and click Add at the bottom. Then zip the contents of job folders and project and upload it.

5. What is the significance of Azure scheduler in the context of WebJobs?

To schedule a WebJobs, we need to have Azure scheduler enabled.

6. What is the cost for running WebJobs?

As of now, three is no additional cost for using Azure WebJobs unless AlwaysOn feature is turned on.

7. What is the maximum allowed file size for content (.zip files) submitted to WebJobs?

The maximum allowed file size for the content (.zip files) submitted to WebJobs is 100 MB. The .zip file should contain the executables (in any one of the formats like .exe, .cmd, .bat, .sh, .php, .py and .js).

 

 

 

 

How to create a SharePoint 2013 Site by invoking SP 2013 REST endpoints

In this post, we would see on how to create a SharePoint 2013 site using the SharePoint 2013 RESTful endpoints available in HostWeb. I would be using a SharePoint hosted app to demonstrate this scenario.

File –> New –> Office/SharePoint –> Apps and name it as ‘CreateSiteApp’.

image

In the SharePoint hosted app, I’d be making a call to the /_api/web/webinfos/add available in HostWeb to create a sub-site under HostWeb.

Open App.js file.

Copy and paste the following code. The following snippet leverages Cross Domain library of SharePoint 2013 to make

'use strict';

var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();
var HostWebUrl;
var AppWebUrl;



// This code runs when the DOM is ready and creates a context object which is needed
 to use the SharePoint object model
$(document).ready(function () {
    HostWebUrl =
                decodeURIComponent(
                    RetrieveQueryStringParameter("SPHostUrl")
            );
    AppWebUrl =
        decodeURIComponent(
            RetrieveQueryStringParameter("SPAppWebUrl")
    );

    var scriptbase = HostWebUrl + "/_layouts/15/";
    $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);


});

function RetrieveQueryStringParameter(ParamsforRetrieval) {
    var params =
        document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == ParamsforRetrieval)
            return singleParam[1];
    }
}


function execCrossDomainRequest() {

    var executor = new SP.RequestExecutor(AppWebUrl);
    executor.executeAsync(
        {
            url:  AppWebUrl +"/_api/web/webinfos/add",
            type: "POST",
            data: JSON.stringify(
                {
                    'parameters': {
                        '__metadata': { 'type': 'SP.WebInfoCreationInformation' },
                        'Url': 'SundarSite',
                        'Title': 'SundarSite',
                        'Description': 'Site created using REST',
                        'Language': 1033,
                        'WebTemplate': 'sts',
                        'UseUniquePermissions': false
                    }
                }
            ),
            headers: {
                "accept": "application/json; odata=verbose",
                "content-type": "application/json;odata=verbose",
                "content-length": 1028,
                "X-RequestDigest": $("#__REQUESTDIGEST").val()

            },
            success: successHandler,
            error: errorHandler
        }
        );




       

}

function successHandler(data) {
    document.getElementById("message").innerText =
        "Site Created Successfully";

}

function errorHandler(data, errorCode, errorMessage) {
    document.getElementById("message").innerText =
        "Could not complete cross-domain call " + errorMessage;
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 Subscribe to my blog