How to recreate a view by xml?
-
31-12-2020 - |
Pregunta
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);
Solución
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.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a sharepoint.stackexchange