Search code examples
sharepointsharepoint-2010sharepoint-api

SharePoint 2010: Updating View Xml (Enabling Toolbar)


I am trying to enable View toolbar on my list views. But it seems not to be working. How can I get the code right?

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

UPDATE:

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

Closer, but still not good.

This time, it did modified the view. I know it because, the second time I run the debugger, I did noticed the value of the SPView.Toolbar property changed.

But when I open the same page in the SharePoint designer, I do not see <Toolbar Type="Standard" ShowAlways="TRUE"/> but I see the default value as: <Toolbar Type="Standard"/>.

In addition to that, I do not see the toolbar on the list view page when I open the list view in the browser.


Solution

  • This is what I had to do to make it work.

    /// <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;
            }
        }
    }