Sharepoint web part: type could not be found/registered as safe - sharepoint

I have a SharePoint web part (essentially just a "Hello World" app) that I just created and am having a problem deploying it. I have signed the .dll, created the .dwp, and registered it as a safe control in web.config. I am able to add it to the Web Part Gallery and add the details for it; however, when I attempt to add it to a page, I get the following error:
A Web Part or Web Form Control on this
Page cannot be displayed or imported.
The type could not be found or it is
not registered as safe.
Following is my .dwp file:
<?xml version="1.0"?>
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
<Assembly>SimpleWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=################</Assembly>
<TypeName>MyWebParts.SimpleWebPart</TypeName>
<Title>My Simple Web Part</Title>
<Description>A simple Web Part</Description>
</WebPart>
and the entry I added to web.config:
<SafeControl Assembly="SimpleWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=################" Namespace="MyWebParts" TypeName="*" Safe="True" />
I also tried using wildcards for the namespace, which didn't help. I have even tried setting the web.config trust level to "Full" (which I would never do in production, but tried to attempt to narrow down the problem) and still had no luck. Any ideas? Thanks.

One possibility is that types names are not in sync with the .webpart file. For ex.
In the .webpart file:
<?xml version="1.0" encoding="utf-8"?>
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="Namespace.Class1, $SharePoint.Project.AssemblyFullName$" />
and in the .cs file:
namespace Namespace
{
public class Class2 : WebPart
and in the SharePointProjectItem.spdata file:
<SafeControl Assembly="Class2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9385058ce1ee51a9" Namespace="Namespace" TypeName="*" Safe="True" SafeAgainstScript="False" />
you get the idea: triple-check names consistency across all project artifacts.

Are you deploying your webpart using a SharePoint Solution (.wsp file)? Check out WSPBuilder if you aren't. We also use SharePoint Installer Between the two, a lot of the problems (whether stemming from human error or otherwise) in deployments, like your problem have been resolved in our environment.

Make sure that you have the Web Part class as Public, might sound silly but I faced once. Also try to populate the web part from the Web part Gallery .

I also saw this problem when I changed the namespace for my WebPart assembly, and the namespace wasn't updated in all of the source files in the solution.

Did you try deploying it to the GAC?

Ok , this may be very late but will be useful for the rest. I created a webpart and was getting the error "A Web Part or Web Form Control on this Page cannot be displayed or imported. The type could not be found or it is not registered as safe."
I figured that I was using a Chart inside this webpart and once I removed the reference of the chart, it worked. So, the Chart control has to be also marked as typesafe.
Make sure all the references in the webpart are type safe.

This usually happens when you change the name of the web part from VisualWebPart1 to MyNewWebPart. After doing a Search and Replace over the entire solution, rename all of the files and folders with VisualWebPart1 to MyNewWebPart. The problem is caused by the file SharePointProjectItem.spdata not being updated. Open SharePointProjectItem.spdata and replace VisualWebPart1 with MyNewWebPart. This fixes the problem in most cases.
Open every file in the solution and verify that there are no references to VisualWebPart1. If there are, change them manually to MyNewWebPart.

Geo,
It's been a while since I worked with Web Parts but I laid out the steps here:
http://www.codersbarn.com/?tag=/webpart
Maybe there's something there that can help.
Anthony :-)

As Tim Scarborough already mentioned, this can happen if you change the namespace and don´t update all source files (as this is not done automatically). Just for illustration, in my case the problem was that the webpart class had a new namespace a.b but I forgot to update this in the webpart file:
<?xml version="1.0" encoding="utf-8"?>
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
//update below!
<type name="a.b.yourClass, $SharePoint.Project.AssemblyFullName$" />
<importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>
</metaData>
<data>
<properties>
<property name="Title" type="string">your Title</property>
<property name="Description" type="string">your Description</property>
</properties>
</data>
</webPart>
</webParts>

Well, if you have already found the solution, that's great. But here is a tip for someone who is searching for more. If you added a new webpart or a visual webpart and just replace the namespace in the files such as code files or Element.xml or even .webpart file you might still face this issue. The reason is, as mentioned somewhere above, the namespace was not changed in .spdata file, which is a file created by Visual studio itself. Just to be sure go check in the Virtual Directories in the inetroot. If the namespace is still the older one, do a search in your Solution and find out the conflicting one. Most probably in the project.spdata file, which everyone tends to ignore.

Related

Sharepoint webpart deployment - activate features error

I'm a noob to Sharepoint development, actually I hoped I'd always be a noob to it, and I'm having difficulty building my first web part for a Sharepoint 2010 website. I'm using Visual Studio 2010, and I started with a Visual Web Part. All i added was a simple asp:Label control, as I just want to make sure it's deploying correctly before I go any further. It builds, says it's deploys even, and then croaks on the 'activate feature' step. I get the following error.
Error 1 Error occurred in deployment step 'Activate Features': Feature with Id '12765e82-6e4a-4407-aa8c-77b537841f4b' is not installed in this farm, and cannot be added to this scope. 0 0 scCodePart
There isn't much out there to go by for help on this. I made sure the scope of the feature was set to 'Site', as was recommended. I even tried changing the xml of Feature1.Template.xml to:
<?xml version="1.0" encoding="utf-8" ?>
<Feature Id="12765e82-6e4a-4407-aa8c-77b537841f4b" Scope="Site" xmlns="http://schemas.microsoft.com/sharepoint/">
</Feature>
And the error persists. I read something about the web.config of the site might causing it, but it provided no explanation as to why. Any thoughts?
EDIT
There is my Elements.xml:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/" >
<Module Name="scClosetCodesVWP" List="113" Url="_catalogs/wp">
<File Path="scClosetCodesVWP\scClosetCodesVWP.webpart" Url="scClosetCodesVWP.webpart" Type="GhostableInLibrary" >
<Property Name="Group" Value="Custom" />
</File>
</Module>
</Elements>
and my Feature1.Template.xml:
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/">
</Feature>
I haven't altered either one of them from what Visual Studio initially creates. I tried declaring the id & scope in Feature1.Template.xml, but i got the same error after.
kindly go through the following URL which has complete solution for your problem
http://blogs.msdn.com/b/sowmyancs/archive/2008/04/02/server-error-feature-guid-is-not-installed-in-this-farm-and-cannot-be-added-to-this-scope.aspx
It sounds like you have the incorrect scope set for your feature. Take a look here to see what features can be deployed at what scope.
Simplest way is just to weak that scope setting, redeploy and see if you get a better result :)
if you have farm admin privilages, you can goto the Central Admin -> System Settings -> Manage Farm Solutions
Select Project.wsp file and click on Deploy Solution
Depending the feature scope setting, go there to activate it.
For Example, if its site collection level, goto site collection settings and activate it.
Alternately you can also use powershell to do the same thing.

IIS 7 Configuration Paths

I am trying to make my c++ ahadmin application compatible to IIS 7. My app needs to read the websites configuration (via metabase properties in IIS 6).
I read a lot of articles about the configuration paths and I think I have a good idea of how it work - however I am not sure of one thing:
To get to the configuration, I can either commit the MACHINE/WEBROOT/APPHOST/ path or the MACHINE/WEBROOT/APPHOST/Default Web Site.
I understand that the latter refers to the actual web.config of the specific website, and the former refers to the general applicationHost.config file, in which general settings are set.
My app doesn't know however whether a web.config file exists.
My question: if I want to get to this path - Object.ConfiguredObject.Site.Bindings, do I need to commit the APPHOST path or the APPHOST/Default Web Site path?
How do I know that in runtime?
You will always commit your bindings to MACHINE/WEBROOT/APPHOST.
You should go have a look at the schema files in:
%systemroot%\System32\inetsrv\config\schema
They will help you identify where settings should belong.
Update:
Per your comment:
So for example, AccessSSLFlags would
be mapped to
ConfigurationSection.AccessSection.SslFlags
- what section will I commit in that case? How do I know which section I
need to commit?
That all depends. IIS7 supports a mechanism called Feature Delegation. If a feature is delegated then this means a user can configure that feature in their local web.config. Some features are configured under system.webServer, others system.web.
What a user can and can't configure locally in his/her web.config is controlled by entries in two files:
%systemrooot%\system32\inetsrv\config\administration.config
%systemrooot%\system32\inetsrv\config\applicationHost.config
If you go and look at the IIS7 configuration schema in:
%systemroot%\System32\inetsrv\config\schema\IIS_schema.xml
What you'll find is that there are two main types of section:
system.applicationHost/xxxx
system.webServer/xxxx
Anything that is configurable under system.applicationHost is generally not considered a user modifiable configuration item. In fact if you open applicationHost.config you will see:
<sectionGroup name="system.applicationHost">
<section name="applicationPools" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
<section name="configHistory" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
<section name="customMetadata" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
<section name="listenerAdapters" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
<section name="log" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
<section name="serviceAutoStartProviders" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
<section name="sites" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
<section name="webLimits" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
</sectionGroup>
Notice the allowDefinition="AppHostOnly"? That's basically telling you that these settings can't be configured in web.config.
The scope of how feature delegation works is far too wide to cover in an answer so I suggest you read the article linked to above.
It sounds like you are trying to build a generic tool to manage configuration, and so you might want to consider to follow a similar pattern that IIS Manager follows; in short, it always tries to save configuration to the deepest path possible. What this means is that it will always Commit it to the place where it can, by looking at if the section is locked or not. It uses Managed code (Microsoft.Web.Administration), but you can access the same data from C++ using AppHostElement.GetMetadata("isLocked").
By the way if you are using C++, I would STRONGLY recommend using AHADMIN directly (and not WMI, or anything else), in particular IAppHostWritableAdminManager.
So the algorithm would be, set the CommitPath to the same value as the GetAdminSection configuration path specified. Then check for IsLocked, if it is then remove the last "path part" (trim starting the last '/'), and read again till you find the place where the section is unlocked. That is the deepest place where you can save it. Also, you will need to switch to MACHINE/WEBROOT at some point if it is a system.web section. IsLocked will respect things like Section Definition allow location, and other things that are required. If you want to make it bullet proof you would even need to check for attribute-level locking, but I think that is quite advanced.

The solution contains no Web application scoped resource,

I need little help regarding sharepoint solution set up in sharepoint.
I created wspbuilder project(12 hive structure including controltemplates folder)
I have created project for user controls(like login logout etc) and when I build them the .ascx files are being added to 12\controltemplates folder(I wrote postbuild event to add .ascx as controltemplate)
There are no compilation errors. I built wsp and added it to solution store. But when I am trying to deploy it to the specific web application I can see no selection of web application in deploysolution window.
It is saying
The solution contains no Web application scoped resource, and therefore cannot be deployed to a particular Web application. It can only be deployed globally.
I think the problem is adding safecontrols to the manifest.xml. When I build the wsp no safe controls are adding to the maifest.xml. I included deploymenttarget to GAC in wspbuilder.exe.config file also.
my feature.xml is as follows
<Feature Id="DBF94C51-A4AB-4c47-BD97-74D3795C6A63"
Title="site feature"
Description="My sharePoint features"
Version="1.0.0.0"
Scope="Site"
Hidden="FALSE"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/"
ReceiverAssembly="[[4part assembly name]]"
ReceiverClass="[[Receiver class]]"
>
How I can resolve this issue. I want to deploy the wsp to specific webapplication only.
Thank you.
if you are registering safecontrols, you need to scope the feature at the web application level so it know which web.config to update. Change the scope to WebApplication and it will know which web.config to deploy to.
Also when using stsadm use the -url switch to supply the web application you need.
Shane
How are you deploying this - what are the exact STSADM commands you are issuing? Do they match the scope in your Manifest file?
http://msdn.microsoft.com/en-us/library/bb861828(office.12).aspx
ohh actually it was my mistake..sorry for the disturbance.
I didn't include the key value in the wspbuilder.exe.config file. I am taking the safe controls into other specified folder in the solution. I had to include in the config file.
Now everything is fine. Thanks for the help.
I did kind of same observation as you.
I don't think it is related to gac or bin deployment of the dll, but only if there is safecontrol included.
Here is how to do it in SP2010:
http://rasor.wordpress.com/2011/12/04/sp2010-wsp-global-or-not/

WSPBuilder and Code behind for a Sharepoint Masterpage

I created a code behind file for a custom master page in visual studio. I hooked everything up manually; safe control and custom cas policy. Everything works great!
I then wanted to put this into a sharepoint solution using WSPBuilder for better deployment. I created WSP solution, added my class file and changed the output directory to the bin folder. I then built the solution and deployed it, making sure to change the page directives on the master page to reflect the new assembly name.
Now when I go to view the sharepoint site I get an error stating Security Exception error stating
‘Exception Details: System.Security.SecurityException: That assembly does not allow partially trusted callers.’
This has me stumped as it works as a visual studio class file deployed to the bin directory of the website.
However when I put this into a sharepoint solution it breaks! I tried adding
‘[assembly: System.Security.AllowPartiallyTrustedCallers]’
to the AssemblyInfo.cs but this hasn’t helped.
Anyone else experinced this or have any advice?
EDIT: I should also mention that the code behind is trying to access a sharepoint list.
Don´t you still have to include the SafeControls entry in order for it to work, like:
<SafeControl Assembly="[FullAssembly Name]"
Namespace="[YourMasterPageNamespace]"
TypeName="*"
Safe="True" />
or in WSPBuilder config:
<add key="BuildSafeControls" value="True" />
Never seen this.. but I suspect not many people have created codebehinds to the master pages in SharePoint (Microsoft doesn't too!).
I don't know what you are trying to build but I'd probably implement it using a server control that is included on the master page.
AllowPartiallyTrustedCallers has always fixed it for my server controls.
What is the trust in your web.config file set to? Try Full.
Are you calling a third party assembly?
I ran into a situation recently that I was using a third party assembly and it did not have AllowPartiallyTrustedCallers in its code. When I tried to use the assebmly, it would fail.
Are you sure that the assembly has been deployed to bin and no to GAC by accident? If there are two assemblies the one in GAC takes precedence.
You might try checking that you are using the fully qualified five part name including the correct public key token and namespace for your assemblies.

Cannot find ajax extensions in MOSS 2007

I have two questions here:
1) I am trying to implement AJAX in sharepoint.
I have modified web.config as referred by:
http://sharepoint.microsoft.com/blogs/mike/Lists/Posts/Post.aspx?ID=3
but still i am getting the following error :
Unknown server tag 'asp:ScriptManager'. at System.Web.UI.TagPrefixTagNameToTypeMapper.System.Web.UI.ITagNameToTypeMapper.GetControlType(String tagName, IDictionary attribs)
at System.Web.UI.MainTagNameToTypeMapper.GetControlType2(String tagName, IDictionary attribs, Boolean fAllowHtmlTags)
at System.Web.UI.MainTagNameToTypeMapper.GetControlType(String tagName, IDictionary attribs, Boolean fAllowHtmlTags)
2) Secondly, i am trying to implement adrotator in sharepoint. I have tried installing the SPAdrotator but i am not able to display the images when i give the image library name as source in the "query" section.
For Point #1:
Have you added this assembly to the GAC?: System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
The problem is that it can't find the scriptmanager class, which says to me that the assembly isn't installed properly. If it's in the bin directory of the SharePoint site, you would need to change the trust level (not recommended) from the WSS_Minimal default.
Are you using .NET 2.0 or 3.5? The extensions downloaded for 2.0 are the much same as the ones included in 3.5, but the references in web.config will be different.
Easiest way to get the right details is to create a new ajax project in visual studio and copy bits from the web.config that creates for you.
2 should probably be posted as a seperate question, but it may have something to do with internal and display names.

Resources