2

I am trying to show the Add New Item link on an existing XlstListviewwebpart. But the code below is not working. I found this here: http://suryapulipati.blogspot.be/2011/08/sharepoint-2010-show-add-new-item-or.html

but toolbarnode is always null enter image description here

private void ShowAddOrganisationLinkOnHomePage(string webUrl)
        {
            Logger.LogDebug("NLSponsoringSiteConfigSponsoringCentralEventReceiver", "ConfigureOrganisationDashboard(SPWeb currentWeb)", "BEGIN");           
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPFile pageOrganisation = null;
                using (SPWeb oHomeWeb = new SPSite(webUrl).OpenWeb())
                {
                    try
                    {                       
                        pageOrganisation = oHomeWeb.GetFile("default.aspx");
                        pageOrganisation.CheckOut();
                        string idWebPartRootOrganisation = "ID_RootOrganisation";
                        oHomeWeb.AllowUnsafeUpdates = true;
                        using (SPLimitedWebPartManager mgrPageManager = pageOrganisation.GetLimitedWebPartManager(PersonalizationScope.Shared))
                        {
                            SPList organisations = oHomeWeb.GetSafeListByName(SponsoringCommon.Constants.LISTNAMES_ORGANISATIONS2);
                            XsltListViewWebPart lvwpOrganisation = mgrPageManager.WebParts[idWebPartRootOrganisation] as XsltListViewWebPart;
                            SPView oCustomView = organisations.Views[SponsoringCommon.Constants.VIEWS_ORGANISATION_DASHBOARD_NAME];

                            PropertyInfo nodeProp = oCustomView.GetType().GetProperty("Node", BindingFlags.NonPublic | BindingFlags.Instance);
                            XmlNode node = nodeProp.GetValue(oCustomView, null) as XmlNode;
                            XmlNode toolbarNode = node.SelectSingleNode("Toolbar");
                            if (toolbarNode != null)
                            {
                                if (!toolbarNode.Attributes["Type"].Value.Equals("Freeform"))
                                {
                                    toolbarNode.Attributes["Type"].Value = "Freeform";
                                }
                            }
                            oCustomView.Update();
                            mgrPageManager.SaveChanges(lvwpOrganisation);                          
                        }
                    }
                    catch (Exception)
                    {
                        oHomeWeb.AllowUnsafeUpdates = false;
                        if (pageOrganisation != null) pageOrganisation.UndoCheckOut();
                        throw;
                    }
                    finally
                    {
                        pageOrganisation.CheckIn(string.Empty);
                        oHomeWeb.AllowUnsafeUpdates = false;
                    }                       
                }
            });
        }
  • I think it shows it by default > – Mathematics Sep 5 '12 at 12:06
  • also try this link if it helps :- social.msdn.microsoft.com/Forums/en-SG/sharepointdevelopment/… – Mathematics Sep 5 '12 at 12:16
  • @yaron the customer in the past had it hidden, now I need to show it again by code. otherwise I wouldnt be asking this – Luis Valencia Sep 5 '12 at 12:24
  • 1
    not sure if you can show it if in case someone removed it like its been explained in link above but if you couldn't find anyway then you can create a new list with different internal name however same title and transfer all items to that list :) – Mathematics Sep 5 '12 at 12:28
2

I did like this:

using (SPLimitedWebPartManager mgrPageManager = pageOrganisation.GetLimitedWebPartManager(PersonalizationScope.Shared))
                        {
                            SPList organisations = oHomeWeb.GetSafeListByName(SponsoringCommon.Constants.LISTNAMES_ORGANISATIONS2);
                            XsltListViewWebPart lvwpOrganisation = mgrPageManager.WebParts[idWebPartRootOrganisation] as XsltListViewWebPart;
                            Functions.SetToolbarType(lvwpOrganisation, "Freeform");

                            mgrPageManager.SaveChanges(lvwpOrganisation);                          
                        }


 public static void SetToolbarType(XsltListViewWebPart lvwp, string viewType)
        {
            try
            {
                MethodInfo ensureViewMethod = lvwp.GetType().GetMethod("EnsureView", BindingFlags.Instance | BindingFlags.NonPublic);
                object[] ensureViewParams = { };
                ensureViewMethod.Invoke(lvwp, ensureViewParams);
                FieldInfo viewFieldInfo = lvwp.GetType().GetField("view", BindingFlags.NonPublic | BindingFlags.Instance);
                SPView view = viewFieldInfo.GetValue(lvwp) as SPView;
                Type[] toolbarMethodParamTypes = { Type.GetType("System.String") };
                MethodInfo setToolbarTypeMethod = view.GetType().GetMethod("SetToolbarType", BindingFlags.Instance | BindingFlags.NonPublic, null, toolbarMethodParamTypes, null);
                object[] setToolbarParam = { viewType }; //set the type here
                setToolbarTypeMethod.Invoke(view, setToolbarParam);
                view.Update();
            }
            catch { }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.