SharePoint error: “Cannot import Web Part”

前端 未结 11 1439
青春惊慌失措
青春惊慌失措 2021-02-08 02:33

I have a web part that I\'ve developed, and if I manually install the web part it is fine.

However when I have packaged the web part following the instructions on this w

11条回答
  •  臣服心动
    2021-02-08 02:51

    Now I get a answer for similar problem as below:

    When I try to added a new wep part to the page, then sharepoint show me a error message, tell me--Can not import my web part, this error message define in .webpart file.

    So i tried to add some ohter web parts in the page , A strange quesiton appearance, some of them can be added , some of them can not be added.

    After I traced the code of my web part and anaylsis them, I found the reason:

    Old Code for web part ProjectInfo(my web part name) is:

    namespace ProjectInfo
    ....
     public class ProjectInfo:System.Web.UI.WebControls.WebParts.Web.part
     {
            .....
            private SPWeb _spWeb;
            private SPList _spList;
            private string _listName = "ProjectDocs";
            ......
     }
    public ProjectInfo()
     {
            .....
                _spWeb = SPContext.Current.Web;
                //It show me a error here when i trace the code
                _spList = _spWeb.Lists[_listName]; 
            .....
     }
    

    Stop now, I thought that it maybe the web page init order problem. AS web page load web part control, constructrue function ProjectInfo() will be running at first. Actually, the web page havn't finish its init. by the time.

    so i did a test. firstly, I put a good web in the page, it's ok . then, I try to put the web part in the page which can not be added just now. ~~ OK!! It's working ...because the page already init. finished.

    Ok! I corrected my code:

       namespace ProjectInfo
        ....
         public class ProjectInfo:System.Web.UI.WebControls.WebParts.Web.part
         {
                .....
                private SPWeb _spWeb;
                private SPList _spList;
                private string _listName = "ProjectDocs";
                ......
         }
        public ProjectInfo()
         {
                .....
                    //Remove code in constructure function.
                    //_spWeb = SPContext.Current.Web;
                    //It show me a error here when i trace the code
                    //_spList = _spWeb.Lists[_listName]; 
                .....
         }
    
       protected override void CreateChildControls()
            {
             ....
               base.CreateChildControls();
    
                _spWeb = SPContext.Current.Web;
                _spList = _spWeb.Lists[_listName];
              ....
             }
    

    After I test, the error message did't happed again.. LoL ~~

    Hope this explain will help you .

提交回复
热议问题