Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / SharePoint / SharePoint2013

SharePoint 2013: Setting style for list view using RPC

0.00/5 (No votes)
14 Jun 2016CPOL2 min read 7.2K  
This tip shows how to resolve some limitations of CSOM related to list views configuration

Introduction

I am working on task to create and setup SharePoint sites based on some template sites. I must implement this task without server code, so I am using SharePoint client model. But it has a couple of limitations, and one of them is that there is no way to set some properties of list views.

Background

I have looked through the internet for a solution. I have not found it but there was a tip to use SharePoint RPC and a lot of questions on how to do this is not clear.

I decided to investigate it deeper. My investigations was based on:

  1. MSDN article "UpdateView Method"
  2. Use RPC protocol to access WSS v3 site

The main problem was that using RPC UpdateView throws different exceptions and any log entries describing the problem. To figure out the problem, I have analyzed requests and response from SharePoint while edit view using user interface - in fact, SharePoint uses the same method (RPC), but a little different format.

So the solution is below.

Solution

Step 1: Creating UpdateView Request Body

After investigation, I have figured out that UpdateView:

  • should have minimum following parameters:
    • View - this should be GUID in uppercase
    • NewViewName- Title of view (string)
    • NewViewFileName - file name without extension (and path). Even if view is hidden and used for ListViewWebPart (in this case also need set name of file where it located).
    • Level - this parameter should be used only for hidden views and should be set to 255
  • if no other parameters are set, this method removes all view customizations (fields, grouping, etc.)
  • to set style for view use ViewStyle parameter. Value - id of style or "default"
  • if view should contain individual checkboxes, use TabularView parameter. Values are TRUE or FALSE (uppercase).

Example:

XML
<?xml version="1.0" encoding="UTF-8"?>
<Method ID="UpdateView">
  <SetList Scope="Request">{a49a8fd7-ddd6-4384-beac-04dc6c3081cd}</SetList>
  <SetVar Name="Cmd">UpdateView</SetVar>
  <SetVar Name="View">{8DDDAAE3-A5BE-4F06-8819-23FE5C82141B}</SetVar>
  <SetVar Name="NewViewName">View Title</SetVar>
  <SetVar Name="NewViewFileName">AllItems</SetVar>
  <SetVar Name="ViewStyle">20</SetVar>
  <SetVar Name="TabularView">TRUE</SetVar>
</Method>

Step 2: Execute Request

Let's rpcUrl variable contains url in the following format:
http://[server]/[Sites]/][Site_name/]_vti_bin/owssvr.dll?Cmd=DisplayPost

Let's requestBody (string) variable contains XML data with request body:

This way request to SharePoint will looks like:

C#
HttpWebRequest objRequest = (HttpWebRequest)HttpWebRequest.Create(rpcUrl);
objRequest.Method = WebRequestMethods.Http.Post;
objRequest.Credentials = CredentialCache.DefaultNetworkCredentials;
objRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
objRequest.Headers.Add("X-Vermeer-Content-Type", "application/xml");
objRequest.ContentType = "application/xml";
StreamWriter sw = new StreamWriter(objRequest.GetRequestStream());
sw.Write(requestBody);
sw.Close();
HttpWebResponse objResponse = objRequest.GetResponse() as HttpWebResponse;
StreamReader sr = new StreamReader(objResponse.GetResponseStream());
string result = sr.ReadToEnd();
sr.Close();
objResponse.Close();

You can analyze result to understand if request is successful.

Tip 1: In line 3, you can use other credentials, for example SharePointOnlineCredentials.

Tip 2: Line
objRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
is used to support SharePoint Online.

Step 3: Set Another Properties

The code above updates view and sets only some properties. Other view properties (fields, ordering, grouping) becomes empty. You can also set them using this method, but I am using CSOM model to adjust properties (in my case all view properties are stored in internal structures and it is no problem to set them).

Conclusion

MS says that RPC is deprecated, but correctly they self use it for SharePoint Online (2016).

Obviously, this method is tricky and is not graceful. But if at my point it is single method to do this task and it works even in cloud.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)