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;

Upload Forms, VIews to SharePoint using Client Object Model

First thing first, we need to use the namespace, using Microsoft.SharePoint.Client.
byte[] bytes = IO.File.ReadAllBytes(strFilePath);

IO.MemoryStream mStream = new IO.MemoryStream(bytes);

SP.File.SaveBinaryDirect(this.ctx, strServerRelativeUrl, mStream, true);

Remember, SaveBinaryDirect is use to upload Forms, & Views. You can’t upload a ListItem like, Document Library by this process. For other file types, plz check my other blog (will be posting shortly)

Create, Retrieve, and Delete the View of a SharePoint List using Client Object Model

string siteUrl = "http://servername:12345/";
ClientContext ctx = new ClientContext(siteUrl);
List oList = ctx.Web.Lists.GetByTitle("ListTitle");

Retrieve Views

SP.ViewCollection viewCollection = oList.Views;
ctx.Load(viewCollection);
ctx.ExecuteQuery();

Delete all the Views

foreach (SP.View vw in viewCollection)
{
	Microsoft.SharePoint.Client.View view = oList.Views.GetByTitle(vw.Title);
	ctx.Load(view);
	view.DeleteObject(); // Delete Existing views
	ctx.ExecuteQuery();
}

Create a new View

SP.ViewCreationInformation viewCreationInformation = new SP.ViewCreationInformation();
viewCreationInformation.Title = "New View";
viewCreationInformation.ViewTypeKind = SP.ViewType.None;
viewCreationInformation.RowLimit = 30;//Make sure that the name of the fields should be equal to 1 any of the associated SiteColumns Internal Name. The Fields in the view will appear in the same order as declared here.
viewCreationInformation.ViewFields = new string[]{"Title","Created","Modified"};
viewCreationInformation.SetAsDefaultView = true;
oList.Views.Add(viewCreationInformation);

ctx.Load(oList.Views);
ctx.ExecuteQuery();