Saturday, April 16, 2011

Changing the toolbar on ListViewWebPart

Thanks for the post Changing the toolbar on a SharePoint ListViewWebPart, it's not hard any more to show up the "Add new item" link in ListViewWebPart in MOSS 2007. However, can we do the same thing in SharePoint 2010?   Well, just need to change it a little bit.

During my test, I found a strange thing: we need totally different way to specify different settings for "Toolbar". Below is some source code. "SetToolbarTypeFreeForm()" is used to change the toolbar type to "FreeForm" (or "None" if use different type), and "ShowToolbar()" can display the standard toolbar.

        public const string Zone_TitleBar = @"TitleBar";
        public const string Zone_Header = @"Header";
        public const string Zone_LeftColumn = @"LeftColumn";
        public const string Zone_MiddleColumn = @"MiddleColumn";
        public const string Zone_RightColumn = @"RightColumn";
        public const string Zone_Footer = @"Footer";

        public enum ListViewWebPartToolbarType
        {
            Standard = 0,       //Standard
            FreeForm = 1,       //Freeform
            None = 2,           //None
            ShowToolbar = 4     //ShowToolbar
        }


        public static void SetToolbarTypeFreeForm(SPWeb objSPWeb, string strPageUrl, Guid guidStorageKey)
        {
            string storageKey = guidStorageKey.ToString("B").ToUpper();
            uint toolbarType = (uint)EFSimpleReminder.ListViewWebPartToolbarType.FreeForm;

            if (String.IsNullOrEmpty(storageKey) || storageKey == Guid.Empty.ToString())
                throw new Exception("The webpart must be added to the page before this method (SetToolbarType) can be called");

            SPLimitedWebPartManager limitedWebPartManager = null;
            limitedWebPartManager = objSPWeb.GetLimitedWebPartManager(strPageUrl, PersonalizationScope.Shared);

            ListViewWebPart lvwp = null;
            lvwp = (ListViewWebPart)limitedWebPartManager.WebParts[guidStorageKey];

            string listId = lvwp.ListName;
            string viewId = lvwp.ViewGuid;

            // get the SPWebPartManager from SPLimitedWebPartManager
            PropertyInfo webPartManagerProp = typeof(SPLimitedWebPartManager).GetProperty(
              "WebPartManager",
              BindingFlags.NonPublic | BindingFlags.Instance);
            SPWebPartManager webPartManager = (SPWebPartManager)webPartManagerProp.GetValue(limitedWebPartManager, null);

            // get the SPWebPartConnection from SPWebPartManager
            PropertyInfo spWebPartsProp = typeof(SPWebPartManager).GetProperty(
              "SPWebParts",
              BindingFlags.NonPublic | BindingFlags.Instance);
            SPWebPartCollection webParts = (SPWebPartCollection)spWebPartsProp.GetValue(webPartManager, null);

            // Call the ApplyViewToListWebPart method on the SPWebPartConnection
            // internal void ApplyViewToListWebPart(
            //    string storageKey,
            //    string listID,
            //    string viewID,
            //    uint toolbarType,
            //    out uint flags,
            //    out uint baseViewId)
            MethodInfo applyViewToListWebPart = typeof(SPWebPartCollection).GetMethod(
              "ApplyViewToListWebPart",
              BindingFlags.NonPublic | BindingFlags.Instance,
              null,
              new[]{
        typeof(string),
        typeof(string),
        typeof(string),
        typeof(uint),
        typeof(uint).MakeByRefType(),
        typeof(uint).MakeByRefType()      //this is the new parameter with SharePoint 2010
      },
              null);
            object[] parameters = new object[6];
            parameters[0] = storageKey;
            parameters[1] = listId;
            parameters[2] = viewId;
            parameters[3] = toolbarType;

            applyViewToListWebPart.Invoke(webParts, parameters);
        }

        public static void ShowToolbar(SPWeb objSPWeb, string strPageUrl, Guid guidStorageKey)
        {
            SPLimitedWebPartManager manager = null;
            manager = objSPWeb.GetLimitedWebPartManager(strPageUrl, PersonalizationScope.Shared);

            object[] setToolbarTypeParams = { (uint)EFSimpleReminder.ListViewWebPartToolbarType.ShowToolbar };
            ListViewWebPart objListViewWebPart = null;
            objListViewWebPart = (ListViewWebPart)manager.WebParts[guidStorageKey];

            PropertyInfo viewProp = objListViewWebPart.GetType().GetProperty("View", BindingFlags.Instance | BindingFlags.NonPublic);
            SPView webPartView = viewProp.GetValue(objListViewWebPart, null) as SPView;

            MethodInfo setToolbarType = webPartView.GetType().GetMethod("SetToolbarType",
                BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(uint) }, null);
            setToolbarType.Invoke(webPartView, setToolbarTypeParams);

            webPartView.Update();
            manager.SaveChanges(objListViewWebPart);
        }

//Thanks for this post, we can add a list view to web part page easily
        public static ListViewWebPart AddListViewToPage(
            SPWeb objSPWeb,
            string pageUrl,
            SPView objSPView,
            string zoneID,
            int zoneIndex,
            string strWebPartTitle)
        {
            ListViewWebPart objListViewWebPart = null;
            string strListUrl = string.Empty;
            string strListViewWebPartID = string.Empty;
            if (string.IsNullOrEmpty(strWebPartTitle))
                strWebPartTitle = objSPView.ParentList.Title;

            using (SPLimitedWebPartManager spLimitedWebPartManager = objSPWeb.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared))
            {
                using (objListViewWebPart = new ListViewWebPart())
                {
                    objListViewWebPart.ListName = objSPView.ParentList.ID.ToString("B").ToUpperInvariant();
                    objListViewWebPart.TitleUrl = objSPView.ServerRelativeUrl;
                    objListViewWebPart.WebId = objSPWeb.ID;
                    objListViewWebPart.Title = strWebPartTitle;
                    objListViewWebPart.ViewGuid = objSPView.ID.ToString();
                    //objListViewWebPart.DisplayTitle = "abcdefg";

                    spLimitedWebPartManager.AddWebPart(objListViewWebPart, zoneID, zoneIndex);

                    foreach (System.Web.UI.WebControls.WebParts.WebPart objWebpart in spLimitedWebPartManager.WebParts)
                    {
                        if (objWebpart.TitleUrl == objSPView.ServerRelativeUrl)
                        {
                            return (ListViewWebPart)objWebpart;
                        }
                    }
                }

                //return strListViewWebPartID;
                return null;
            }
        }

//Now, we can add a list view to web part page, and then change the toolbar settings.
                objSPList = objSPWeb.Lists.TryGetList(EFSimpleReminder.Configuration_ListTitle);
                if (objSPList == null)
                    throw new ArgumentException("List(" + EFSimpleReminder.Configuration_ListTitle + ") not found.");
                objListViewWebPart = AddListViewToPage(objSPWeb, strPageUrl, objSPList.DefaultView, EFSimpleReminder.Zone_Header, 1, @"Scheduled reminder list");
                EFSimpleReminder.SetToolbarTypeFreeForm(objSPWeb, strPageUrl, objListViewWebPart.StorageKey);

                objSPList = objSPWeb.Lists.TryGetList(EFSimpleReminder.TaskDefault_ListTitle);
                if (objSPList == null)
                    throw new ArgumentException("List(" + EFSimpleReminder.TaskDefault_ListTitle + ") not found.");
                objListViewWebPart = AddListViewToPage(objSPWeb, strPageUrl, objSPList.Views[EFSimpleReminder.View_Portal], EFSimpleReminder.Zone_LeftColumn, 1, @"Reminder Tasks");
                EFSimpleReminder.ShowToolbar(objSPWeb, strPageUrl, objListViewWebPart.StorageKey);

To get the full version of the source code, you can download it from EF SharePoint Simple Reminder  or EF SharePoint Workflow Timer

1 comment: