1

既存のXlstListviewwebpartに[新しいアイテムの追加]リンクを表示しようとしています。しかし、以下のコードは機能していません。私はこれをここで見つけました:http ://suryapulipati.blogspot.be/2011/08/sharepoint-2010-show-add-new-item-or.html

ただし、toolbarnodeは常にnullです ここに画像の説明を入力してください

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

1 に答える 1

2

私はこれが好きでした:

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 { }
        }
于 2012-09-14T07:41:13.177 に答える