SharePoint 2010:以编程方式编辑视图

时间:2010-10-15 12:31:27

标签: visual-studio-2010 sharepoint-2010

正如标题所说,我需要一种以编程方式编辑sharepoint 2010中列表的现有视图的方法。我找到了很多关于创建新视图的示例:

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);

我还找到了一些关于根据显示的字段编辑现有列表的示例:

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

我唯一没有找到的是修改现有视图的方法,使用CAML查询对其进行过滤,方法与我创建它时的上述示例相同

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:3)

好的,这是解决方案!

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();