更新Web部件时出错

时间:2012-01-02 15:49:24

标签: sharepoint-2010 web-parts

我正在开发一个Web部件,我无法在SharePoint服务器上更新而不在webpart库中删除它。然后,我在power shell中运行:

  • 更新-SPSolution ...
  • 禁用-SP功能......
  • 启用SP功能......

我尝试在事件接收器中以编程方式删除库中的Web部件,但这会导致SharePoint失败:

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;

            using (SPWeb web = site.RootWeb)
            {
                SPList list = web.Lists["Web Part Gallery"];

                // go through the items in reverse
                for (int i = list.ItemCount - 1; i >= 0; i--)
                {
                    // format name to look like a feature name
                    string webpartName = list.Items[i].Name;

                    webpartName = webpartName.Substring(0, webpartName.IndexOf('.'));

                    // delete web parts that have been added
                    if (properties.Feature.Definition.DisplayName == webpartName)
                    {
                        list.Items[i].Delete();

                        break;
                    }
                }
            }
        }

知道我做错了什么吗? THX

1 个答案:

答案 0 :(得分:0)

我回来分享解决方案。 以下是在停用功能时从库中删除Web部件的正确代码:

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        try
        {
            int ItemID = -1;
            using (SPSite oSite = (SPSite)(properties.Feature.Parent))
            {
                using (SPWeb oWeb = oSite.RootWeb)
                {
                    SPList oList = oWeb.Lists["Galerie de composants WebPart"];

                    if (oList == null)
                    {
                        // si SharePoint en anglais 
                        oList = oWeb.Lists["Web Part Gallery"];
                    }
                    // si oList == null, alors aucun de traitement
                    // on ne connais pas le nom de la liste 
                    // de la gallerie de web part
                    if (oList != null)
                    {
                        for (int i = 0; i < oList.ItemCount; i++)
                        {
                            if (oList.Items[i].Title.Equals(WebPartTitle))
                            {
                                ItemID = oList.Items[i].ID;
                                break;
                            }
                        }
                        if (ItemID != -1)
                        {
                            SPListItem oItem = oList.GetItemById(ItemID);
                            oItem.Delete();
                            oList.Update();
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            //will throw to and error page
            throw new Exception(String.Format("The has been an error "
            + "removing the WebPart from the Gallery; {0}", e.ToString()));
        }
    }

最后,在事件接收器中添加此代码后,更新Web部件解决方案的过程是:

  • 禁用-SP功能...

  • 卸载-SP功能......

  • 更新-SPSolution ...

  • Install-SPFeature ...

  • 启用SP功能......

希望有所帮助