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;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s