2

Is it possible to change the view style of a view / list view web part using csom (or jsom)?

The View object (get from List.Views.GetViewByXX()) ha a property "StyleId" but it is readonly.

I have tried by setting the Schema Xml of the view (e.g. the tag ) but it does not seem to get reflected.

Is it simply not supported or am I missing something?

I'm using SharePoint 2013 onprem (2016 jan CU).

2
  • what exactly are you trying to do? – Tiago Duarte Nov 15 '16 at 13:52
  • In a view in ui you can set a style for your view, e.g. "Newsletter". Standard setting is "Default". So I'd I create a view or list view web part with csom i want to change the default setting "Default" to e.g. "Newsletter" using csom – Kristian Nov 15 '16 at 14:53
4

I don't think there is any writable property that exposes the StyleID.

But it appears to work if you customize the xml of the view using the property ListViewXml, which IS writable.

The key is to use v.ListViewXml = doc.FirstChild.InnerXml once you've done your changes.

/*
    * 0-Basic Table
    * 14-Document Details
    * 15-Newsletter
    * 16-Newsletter, no lines
    * 17-Shaded
    * 20-Preview Pane
*/

int styleId = 15;

//parse xml
XmlDocument doc = new XmlDocument();
doc.LoadXml(v.ListViewXml);

XmlElement element = (XmlElement)doc.SelectSingleNode("//View//ViewStyle");
if (element == null)
{
    element = doc.CreateElement("ViewStyle");
    element.SetAttribute("ID", styleId.ToString());
    doc.DocumentElement.AppendChild(element);    
}
else
{
    element.SetAttribute("ID", styleId.ToString());
}

v.ListViewXml = doc.FirstChild.InnerXml;
v.Update();
context.ExecuteQuery();
5
  • Have you tested this as I as said tried that already (although it seems now my tag was stripped)? I'll try tomorrow... – Kristian Nov 15 '16 at 17:24
  • 1
    yep. as long as you send the contents of the View tag and not the view tag itself, it appears to work. – Tiago Duarte Nov 15 '16 at 17:25
  • OK I'll try that tomorrow – Kristian Nov 15 '16 at 17:30
  • 2
    It works!! You are a hero - thanks a lot. I think I can use the same method to change the toolbar and calendar overlays - at least I'll give it a try. You saved my day! – Kristian Nov 16 '16 at 7:44
  • As Tiago says, the important part is to use doc.FirstChild.InnerXml when setting the value. If you use the OuterXml you get no error message, but your view is reset to default (only column Title, no sorting, no grouping, etc.). Took me a while to find out why this was happening. – Hinek Mar 30 '17 at 13:24
1

A small follow up to the answer provided by Tiago. The method by changing the ListViewXml is also possible for other attributes that cannot be changed otherwise, e.g. Toolbar, which for some reason does not change using View.Toolbar (at least for me). So I created these helper methods to do the job in general:

private static string ConvertViewXml(string listViewXml)
{
    var doc = new XmlDocument();
    doc.LoadXml(listViewXml);
    return doc.DocumentElement.InnerXml;
}
private static string GetChangedViewXml(string listViewXml, string node, string attribute, string value)
{
    //parse xml
    var doc = new XmlDocument();
    doc.LoadXml(listViewXml);
    XmlElement element = (XmlElement) doc.SelectSingleNode($"//View//{node}");
    if (element == null)
    {
        element = doc.CreateElement(node);
        element.SetAttribute(attribute, value);
        doc.DocumentElement.AppendChild(element);
    }
    else
    {
        element.SetAttribute(attribute, value);
    }
    return doc.FirstChild.OuterXml;
}

The use the methods like this:

view.ListViewXml = GetChangedViewXml(view.ListViewXml, "ViewStyle", "ID", "15");
view.ListViewXml = GetChangedViewXml(view.ListViewXml, "Toolbar", "Type", "None");
view.ListViewXml = ConvertViewXml(view.ListViewXml);

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.