SharePoint 2010:更新View Xml(启用工具栏)

时间:2011-04-15 17:40:50

标签: sharepoint sharepoint-2010 sharepoint-api

我正在尝试在列表视图中启用“视图”工具栏。但它似乎没有奏效。我怎样才能正确使用代码?

/// <summary>
/// Handles the Click event of the btnEnable control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
private void btnEnable_Click(object sender, EventArgs e)
{
    using (SPWeb spWeb = new SPSite(tbSiteUrl.Text).OpenWeb())
    {
        SPList spList = spWeb.Lists[lbLists.SelectedItem.ToString()];
        foreach (SPView spView in spList.Views)
        {
            EnableToolbar(spView.Title, spList.Title, spWeb.Url);
        }
    }
}

/// <summary>
/// Enables the toolbar.
/// </summary>
/// <param name="viewName">Name of the view.</param>
/// <param name="listName">Name of the list.</param>
/// <param name="webUrl">The web URL.</param>
private void EnableToolbar(string viewName, string listName, string webUrl)
{
    SPSecurity.RunWithElevatedPrivileges(() =>
                                             {
                                                 using (var spSite = new SPSite(webUrl))
                                                 {
                                                     using (SPWeb web = spSite.OpenWeb())
                                                     {
                                                         spSite.AllowUnsafeUpdates = true;

                                                         SPList spList = web.Lists[listName];
                                                         SPView spView = spList.Views[viewName];

                                                         XDocument xDocument =
                                                             XDocument.Parse(spView.GetViewXml());

                                                         XElement xElement =
                                                             xDocument.Element("View").Element("Toolbar");
                                                         xElement.RemoveAttributes();
                                                         xElement.SetAttributeValue("Type", "Standard");
                                                         xElement.SetAttributeValue("ShowAlways", "TRUE");

                                                         spView.SetViewXml(xDocument.ToString());
                                                         spView.Update();

                                                         spSite.AllowUnsafeUpdates = false;
                                                     }
                                                 }
                                             });
}

更新:

using (var spSite = new SPSite(webUrl))
{
    using (SPWeb web = spSite.OpenWeb())
    {
        spSite.AllowUnsafeUpdates = true;

        SPList spList = web.Lists[listName];
        SPView spView = spList.Views[viewName];

        spView.Toolbar =
            @"<Toolbar Type=""Standard"" ShowAlways=""TRUE""/>";

        spView.Update();

        spSite.AllowUnsafeUpdates = false;
    }
}

更近,但仍然不好。

这一次,它确实修改了视图。我知道,因为第二次运行调试器时,我注意到SPView.Toolbar属性的值发生了变化。

但是当我在SharePoint设计器中打开同一页面时,我看不到<Toolbar Type="Standard" ShowAlways="TRUE"/>,但我看到默认值为:<Toolbar Type="Standard"/>

除此之外,当我在浏览器中打开列表视图时,我在列表视图页面上看不到工具栏。

2 个答案:

答案 0 :(得分:2)

这是我必须做的才能让它发挥作用。

/// <summary>
/// Enables the toolbar.
/// </summary>
/// <param name="viewName">Name of the view.</param>
/// <param name="listName">Name of the list.</param>
/// <param name="webUrl">The web URL.</param>
private void EnableToolbar(string viewName, string listName, string webUrl)
{
    using (var spSite = new SPSite(webUrl))
    {
        using (SPWeb web = spSite.OpenWeb())
        {
            spSite.AllowUnsafeUpdates = true;

            SPList spList = web.Lists[listName];
            SPView spView = spList.Views[viewName];

            SPLimitedWebPartManager spLimitedWebPartManager = 
                web.GetLimitedWebPartManager(spView.Url, PersonalizationScope.Shared);

            foreach (WebPart webPart in spLimitedWebPartManager.WebParts)
            {
                if (webPart.GetType().Name.Equals("XsltListViewWebPart"))
                {
                    try
                    {
                        MethodInfo ensureViewMethod = webPart.GetType().GetMethod("EnsureView",
                                                                                  BindingFlags.Instance |
                                                                                  BindingFlags.NonPublic);
                        object[] ensureViewParams = {};
                        ensureViewMethod.Invoke(webPart, ensureViewParams);

                        FieldInfo viewFieldInfo = webPart.GetType().GetField("view",
                                                                             BindingFlags.NonPublic |
                                                                             BindingFlags.Instance);

                        var view = viewFieldInfo.GetValue(webPart) as SPView;

                        Type[] toolbarMethodParamTypes = {Type.GetType("System.String")};
                        MethodInfo setToolbarTypeMethod = view.GetType().GetMethod("SetToolbarType",
                                                                                   BindingFlags.Instance |
                                                                                   BindingFlags.NonPublic, null,
                                                                                   toolbarMethodParamTypes, null);
                        object[] setToolbarParam = {"ShowToolbar"};
                        setToolbarTypeMethod.Invoke(view, setToolbarParam);

                        view.Update();
                    }
                    catch { }
                }
            }

            spSite.AllowUnsafeUpdates = false;
        }
    }
}

答案 1 :(得分:1)

请改用SPView.Toolbar属性。该属性需要<Toolbar/> CAML元素并替换底层CAML片段中的特定XML节点。