SharePoint error: “Cannot import Web Part”

前端 未结 11 1426
青春惊慌失措
青春惊慌失措 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:49

    I got this error when I created a base class web part and then inherited a derived class from it. The base class was fine but the derived class failed when I tried to add it to a web page with the same error as the original post. In my case I had to add a public modifier to the class:

    public class DerivedWebPart : BaseWebPart
    

    Also I added a constructor in the derived class to call the base class one - although I think you shouldn't need this really:

        public DerivedWebPart() : base()
        {
        }
    
    0 讨论(0)
  • 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 .

    0 讨论(0)
  • 2021-02-08 02:55

    Figured it out.

    The error message is the one from the .webpart file:

    <?xml version="1.0" encoding="utf-8"?>
    <webParts>
      <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
        <metaData>
          <!--
          The following Guid is used as a reference to the web part class, 
          and it will be automatically replaced with actual type name at deployment time.
          -->
          <type name="7F8C4D34-6311-4f22-87B4-A221FA8735BA" />
          <importErrorMessage>Cannot import Project Filter.</importErrorMessage>
        </metaData>
        <data>
          <properties>
            <property name="Title" type="string">Project Filter</property>
            <property name="Description" type="string">Provides a list of Projects that can be used to Filter other Web Parts.</property>
          </properties>
        </data>
      </webPart>
    </webParts>
    

    The problem is that the original .webpart file was created on a 32-bit system with Visual Studio Extensions for WSS installed.

    However as I'm now on a 64-bit machine VSEWSS is unavailable, and I believe that results in the above GUID not being substituted as I am not using those deployment tools.

    Replacing the GUID with the full type name works.

    So if you encounter the error message from your importErrorMessage node, then check that your type node in the .webpart file looks more like this (unrelated example):

    <type name="TitleWP.TitleWP, TitleWP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5" />
    

    This is in the format: Class, Namespace, Version, Culture, PublicKey

    You can grab that easily from the web.config file associated with your SharePoint instance, as it will be in the safe controls list.

    0 讨论(0)
  • 2021-02-08 02:57

    I had a problem very similar to this, but Guids weren't the problem: my webpart didn't have the CLSCompliannt attribute set to false. Like so:

    namespace MyNamespace
    {
    
        [CLSCompliant(false)]
        [Guid("...")]
        public class MyWidget : MyWebPartBaseClass
        {
    
        }
    }
    
    0 讨论(0)
  • 2021-02-08 03:00

    I have seen this anomaly several times without a good resolution.

    Check that your Assembly, Namespace, Class name is correct EVERYWHERE. This has hung me up more than once.

    Make sure you have a valid SafeControls entry.

    Make sure your .webpart file is valid (let SharePoint create it for you if you can)

    If you are absolutely positive that everything is correct, well then you are stuck in a place that I have been several times. The only thing that I can come up with is that VS is compiling the assembly wrong. The ONLY fix that I have found is to create a new VS project and set it up how you need, then copy THE TEXT of your old CS files into your new CS files...do not copy the files themselves...the goal is to have everything fresh.

    This has worked for me. Good Luck.

    0 讨论(0)
  • 2021-02-08 03:00

    Have you recycled your worker process or reset IIS?

    0 讨论(0)
提交回复
热议问题