3

I can retrieve the xml data of a view which is stored in the [WSS_Content].[dbo].[AllWebParts] table.

example of decompressed content of a view:

<root>
  <ViewFields>
    <FieldRef Name="DocIcon" />
    <FieldRef Name="LinkFilename" />
    <FieldRef Name="Modified" />
    <FieldRef Name="Editor" />
  </ViewFields>
  <Query />
  <RowLimit Paged="FALSE">30</RowLimit>
</root>

Is there any way to create a view using this xml ? Usually I create a view by following code:

var list = Site.Lists.GetByTitle("Test");
var view = new ViewCreationInformation();
view.Title = "NewVIEW";
list.Views.Add(view);
3

If you need to update via direct xml then use SetViewXml method of view. So your code would like this

SPList list = web.Lists["Test"];
SPViewCollection collViews = list.Views;
string strViewName = "NewVIEW";
System.Collections.Specialized.StringCollection collViewFields = new System.Collections.Specialized.StringCollection();
string strQuery = "";

collViews.Add(strViewName, collViewFields, strQuery, 100, true, false,
    Microsoft.SharePoint.SPViewCollection.SPViewType.Grid, false);

SPView view = list.Views[strViewName];
view.SetViewXml("<View>" + //xml truncated for clarity
    "<Query/>" + 
    "<ViewFields>" + 
        "<FieldRef Name=\"DocIcon\"/>" + 
        "<FieldRef Name=\"LinkFilename\"/>" + 
        "<FieldRef Name=\"Modified\"/>" + 
        "<FieldRef Name=\"Editor\"/>" + 
    "</ViewFields>" + 
    "<RowLimit Paged=\"FALSE\">30</RowLimit>" +
"</View>"); 
view.Update(); 

However, the best way is to update individual properties (Query, ViewFields and RowLimit) of the view.

  • I can't find property SchemaXml in ViewCreationInformation class msdn.microsoft.com/en-us/library/office/… – isxaker Aug 30 '17 at 8:38
  • you are right @isxaker. I have update the code with the correct API SetViewXml. Thanks for clarifying it. – Ahmad Zia Aug 30 '17 at 9:53
  • is it client model(dll) of microsoft sharepoint ? – isxaker Aug 30 '17 at 13:31
  • 1
    This is a server object model. I am not sure if view creation using direct xml is possible in CSOM. If you really need CSOM then first map the xml to object by creating classes with [DataContract] and [DataMember] attributes. Use ViewCreationInformation and fill up ViewFields, RowLimit members with your DataMember properties. – Ahmad Zia Aug 30 '17 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.