Create a SharePoint View with Custom Url Client Object Model C#

In Client Object Model, you don’t have any option to set the url of the view that you’re creating. The url is automatically set as per the Title of your View. So if your Views title is say, My View, then its url will become My%20View. So how you can set the url as MyView instead of the My%20View?

Well, there’s an easy way-out. While creating the View, set its Title to the value that you want its url to have. So, for the above ex, create the view with the title MyView, so that its url will become MyView. Then afterwards, once the view is created, rename the title as you wish say, My View, your url however will not change and you’ll get your new view with the custom title at the desired location.

ClientContext ctx = new ClientContext(weburl);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord); 

List list = ctx.Web.Lists.GetByTitle("ListTitle");
SP.ViewCreationInformation viewCreationInformation = new SP.ViewCreationInformation();

//setting the title as per the desired url
viewCreationInformation.Title = "MyView";
viewCreationInformation.ViewTypeKind = (SP.ViewType)Enum.Parse(typeof(SP.ViewType), "HTML", true);
viewCreationInformation.RowLimit = 30;
viewCreationInformation.Query = "";
viewCreationInformation.SetAsDefaultView = true;

View newView = list.Views.Add(viewCreationInformation);

this.ctx.Load(list.Views);this.ctx.ExecuteQuery;

//set your custom title once your view has been created at the desired location/url
newView.Title = "My View";
newView.Update();
this.ctx.ExecuteQuery;

Create a SharePoint Site using Client Object Model C#

Required namespace:-
using Microsoft.SharePoint.Client;using Microsoft.Online.SharePoint.TenantAdministration; 

Following is the full code sample for creating a new sitecollection in SharePoint using CSOM.

bool rootSiteExist;
SP.ClientContext ctx = new ClientContext(siteUrl);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord);
this.ctx.Load(this.ctx.Web);

try
{
    ctx.ExecuteQuery();
    rootSiteExist = true;
}
catch (Exception)
{
    //Indicates the given sub site is missing
    //This block will be executed when the given Web is not found on the server
    
    rootSiteExist = false;
}

//Create a Site if there is no existing site with the same url.
if(!rootSiteExist)
{
    string targetURL = "https://-admin.sharepoint.com";
    SP.ClientContext adminCtx = new SP.ClientContext(siteUrl);
    adminCtx.Credentials = new SharePointOnlineCredentials(userName, passWord);

    Tenant tenant = new Tenant(context);
    adminCtx.Load(tenant, t => t.ResourceQuota, t => t.ResourceQuotaAllocated, t => t.StorageQuota, t => t.StorageQuotaAllocated);
    adminCtx.ExecuteQuery();

    double avaialableResource = tenant.ResourceQuota - tenant.ResourceQuotaAllocated;
    long availableStorageResource = tenant.StorageQuota - tenant.StorageQuotaAllocated;

    long storageMaximumLevel = 1000; //say
    double userCodeMaximumLevel = 300 //say

    if (availableStorageResource >= storageMaximumLevel)
    {
        if (avaialableResource >= userCodeMaximumLevel)
        {
            bool retryAttemted = false;
            while (true)
            {
                var properties = new SiteCreationProperties()
                {
                    CompatibilityLevel = compatibilityLevel,
                    Url = siteUrl,
                    Owner = owner,
                    Template = template,
                    StorageMaximumLevel = storageMaximumLevel,
                    UserCodeMaximumLevel = userCodeMaximumLevel,

                };
                tenant.CreateSite(properties);
                adminCtx.Load(tenant);

                try
                {
                    /*If you get an error here it means, a root site with the same url is lying in the RecycleBin of the admin Site Collection. In that case
you have to first remove the deleted site before proceeding.*/
                    
                    adminCtx.ExecuteQuery();
                    break;
                }
                catch (SP.ServerException)
                {
                    if (retryAttemted)
                        throw;

                    tenant.RemoveDeletedSite(siteUrl);
                    connect.adminCtx.ExecuteQuery();
                    retryAttemted = true;
                }
            }

            /*Now trying to connect to the newly cretaed Site with the given Url. The site may not show up immediately, hence constantly trying to connect to the site after a given time interval (wait period).*/
            
            int retryCount = 0;
            SP.Web web = null;
            
            while (true)
            {
                try
                {
                    ctx = new ClientContext(siteUrl);
                    ctx.Credentials = new SharePointOnlineCredentials(userName, passWord);

                    web = this.ctx.Web;
                    this.ctx.Load(web);
                    this.ctx.ExecuteQuery();
                    
                    break;
                }
                catch (Exception)
                {
                    ++retryCount;
                    if (retryCount <= 20)
                    {
                        Thread.Sleep(200000);
                        continue;
                    }
                    break;
                }
            }
        }
        else
        {
            //Resources are inadequate
        }
    }else    
    {
        //Storage Size is inadequate
    }
}