自定义 Sharepoint 中的页面视图

发布于 2024-07-17 07:33:43 字数 1184 浏览 3 评论 0原文

我自定义页面文档库中显示的字段(单击站点操作 - >所有站点内容 - >页面显示的表格)。

比我更了解共享点的人建议我也许应该在激活功能时以编程方式从默认视图中删除我不想要的字段,所以我编写了这段代码,感觉非常不优雅解决方案,也不起作用。

SPWeb web = properties.Feature.Parent as SPWeb;

        if (web != null)
        {
            SPList list = web.Lists["Pages"] as SPList;
            if (list != null)
            {
                foreach (SPField field in list.Fields)
                {
                    if (field.Title != "Type" &&
                        field.Title != "Name" &&
                        field.Title != "Modified" &&
                        field.Title != "Checked Out To" &&
                        field.Title != "Page Layout")
                    {
                        if (list.DefaultView.ViewFields.Exists(field.InternalName))
                        {
                            list.DefaultView.ViewFields.Delete(field);                                
                        }
                    }
                }

                list.DefaultView.Update();
            }
        }
    }

该代码肯定是在激活该功能时执行的,所以我显然做错了什么。 我已经找到了这个问题的解决方案,所以如果我错过了谷歌或这个网站上非常明显的东西,我深表歉意。

I'm customize the fields displayed in the Pages document library (the table displayed clicking site actions->all site content->pages).

It was suggested by someone who knows more about sharepoint than me that I should perhaps remove the fields which I didn't want from the default view programmatically on the activation of a feature, so I've written this code which feels like a very unelegant solution, and also does not work.

SPWeb web = properties.Feature.Parent as SPWeb;

        if (web != null)
        {
            SPList list = web.Lists["Pages"] as SPList;
            if (list != null)
            {
                foreach (SPField field in list.Fields)
                {
                    if (field.Title != "Type" &&
                        field.Title != "Name" &&
                        field.Title != "Modified" &&
                        field.Title != "Checked Out To" &&
                        field.Title != "Page Layout")
                    {
                        if (list.DefaultView.ViewFields.Exists(field.InternalName))
                        {
                            list.DefaultView.ViewFields.Delete(field);                                
                        }
                    }
                }

                list.DefaultView.Update();
            }
        }
    }

The code definately executed on activation of the feature so I'm obviously doing something wrong. I've searched for a solution to this so I apologise if I've missed something on google or this site which is blindingly obvious.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2024-07-24 07:33:43

这是因为对 list.DefaultView.ViewFields 的更改超出了范围。 在调用 Update() 之前,正在从数据库刷新集合。
尝试:

if (list != null)
{
    SPView view = list.DefaultView;

    foreach (SPField field in list.Fields)
    {
        if (field.Title != "Type" &&
            ...
            field.Title != "Page Layout")
        {
            if (view.ViewFields.Exists(field.InternalName))
            {
                view.ViewFields.Delete(field);                                
            }
        }
    }

    view.Update();
}

This is because the changes to list.DefaultView.ViewFields are going out of scope. The collection is being refreshed from the database before the call to Update() is reached.
Try:

if (list != null)
{
    SPView view = list.DefaultView;

    foreach (SPField field in list.Fields)
    {
        if (field.Title != "Type" &&
            ...
            field.Title != "Page Layout")
        {
            if (view.ViewFields.Exists(field.InternalName))
            {
                view.ViewFields.Delete(field);                                
            }
        }
    }

    view.Update();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文