SharePoint 2010: edit a view programmatically

As the title says, I need a way to programmatically edit an existing list view in sharepoint 2010. I found many examples of creating a new view:

SPList documents = web.Lists["Documents"];
StringCollection fields = new StringCollection();
fields.Add("Type");
fields.Add("Name");
fields.Add("File Size");
fields.Add("Modified");
fields.Add("Modified By");
fields.Add("Version");
var query = new XElement("Where",
    new XElement("Eq",
        new XElement("FieldRef", new XAttribute("Name", "Project")),
        new XElement("Value", new XAttribute("Type", "Lookup"), "alpha")
    )
).ToString(SaveOptions.DisableFormatting);

SPView view = documents.Views.Add(
    "ProjectFilter",
    fields,
    query,
    100,
    false,
    false,
    Microsoft.SharePoint.SPViewCollection.SPViewType.Html,
    false);

I also found some examples of editing an existing list by display fields:

SPList documents = web.Lists["Documents"];
SPview view = documents.Views["All Documents"];
view.ViewFields.Add("Price");
view.Update();

The only thing I didn't find is a way to modify an existing view filtering it with a CAML request, in the same way as above when I created it

Is there any way to do this?

+3
source share
1 answer

Ok, here is the solution!

SPList documents = web.Lists["Documents"];
SPView docview = documents.Views["Project Filtered"];

var docquery = new XElement("Where",
    new XElement("Eq",
        new XElement("FieldRef", new XAttribute("Name", "Project")),
        new XElement("Value", new XAttribute("Type", "Lookup"), "alpha")
    )
).ToString(SaveOptions.DisableFormatting);

docview.Query = docquery;
docview.Update();
+3
source

Source: https://habr.com/ru/post/1769734/


All Articles