Friday, May 17, 2013

Deal easily with Safe Controls problems in SharePoint with Visual Studio 2010

You get the following error when you try to add a custom WebPart in to your site:

"Type is not registered as safe"

Error Message

This means that you:

  1. Don't have an web.config entry for your dll
  2. have a wrong web.config entry for your dll

The 2nd reason is more often, especially when you develop a WebPart in Visual Studio and change the default Class or NameSpace names.

Steps to solve the problem

1. Check your web.config

This step is only to check if there is a web.config entry when you deploy your solution. For this deploy your solution into the farm. Open your web.config file in (C:\inetpub\wwwroot\wss\VirtualDirectories\youwebapp\web.config).

Search after "<SafeControls>" and check if your dll is listed there and if so check if all parameters like PublicKeyToken, NameSpace or DLLName are correct

Safe Control Entry in Web.Config

Tip: If oyu deployed your assembly into the GAC, you can check the "PublicKeyToken" in your GAC. Just press "Win + R" key and enter "assembly". Search your dll and check the properties with a right click.

2. Check the package in Visual Studio

In a second step you can check the package which will be deployed with the wsp. So open your Visual Studio and click on "Package". The package manifest will appear.

Packaging XML

Check in the manifest XML if your custom dll is listed correctly. You can overwrite this generated xml manually but I don't recommend that. Mostly there is an option somewhere which generates the stuff.

But if you don't trust Visual Studio here the steps to overwrite the XML manually. First click on edit options in the Package view. Then click on the link "Overwrite generated XML and edit manifest in the XML editor".

Scenario 1:

Now you found out that your Safe Control exists but the namespace is wrong. To adjust the setting just click on your module to display the options window. Now edit the "Safe Control Entries" option:

After editing your entries you can check your manifest if your safe control entry is now updated.

Scenario 2:

You can't find your dll because your WebPart is referencing an other dll which is not automatically added to Safe Controls.

You can also add other dlls as Safe Controls. Just open the packaging explorer again and click on advanced at the bottom:

Now you can add any assembly as safecontrol into your package.


Wednesday, May 15, 2013

Different list views for folders, content types and files in SharePoint 2010

There is a not well known feature of SharePoint I want to show. You can create different default list views for different content types.

As an example you have a top folder for specific orders which is automatically created by your customer when he orders something. Within this folder you and your customer exchange documents.

You goal is that when someone enters the order list he only sees his folder.

1. Attempt

My first opinion would be to create a list view with a specific filter. But here begins the problem: How should the filter look like ? If you set "Created By = [Me]" the customer will only see documents and folders he created. When you use "Created By = [Me] & Contenttype = Folder" he would not see subfolders created by others.

2. Attempt

The more elegant way is to use different content types for top folders and subfolders. Based on the content types you can create different default views.

2.1 Create Content Types

So create a new content type based on "Folder" content type. If you wish you can also add some fields to your content type. To create a new folder content type go to your web settings.

Add Site Content Type

Create a new folder content type

Attach the new content type to your list.

  1. Go to your list
  2. Open the list settings page
  3. Click on "Advanced Settings"
  4. Click on "Yes" at "Allow management of content types"
  5. Click "OK"
  6.  A new link "Add from existing site content types" appears on your list settings page. Click it and select your new folder content type.

Now create some folders with the new content type. Do not use the standard folder option from ribbon. Click on the "New Item" or "New Document" ribbon button.

Add folder with custom content type
Within these folders you must use the standard folders for the example to work.

2.2. Create different views

Now we will create 2 default views.  The first view is to show the top folder only to customer which created the folder.
  1. Go to your "Order" list
  2. Modify the standard "AllItems" view.
  3. Apply your filter in the "Filter" section
  4. In the "Folder" Section select "Show items inside folders" & "In the top-level folder"

The second view is for the content inside the top folders. We know that the top folders are created with an other content type than the subfolders. Subfolders are standard SharePoint folders. To create the new view:

  1. Go to your list
  2. Create a new standard view
  3. Click on "Make this the default view". Now you'll have 2 default views.
  4. Apply your filters and sortings
  5. In the "Folders" section now the greyed out 3rd option is available. Click on "In folders of content type" and select your content type.
  6. Click "OK"

If you made everything correctly you'll see in the List Settings page at the bottom following composition:

You have now 2 different views for your content types.

If you open your list you will also notice, that the view for your folder content type is only available within your subfolders.


The is much more with list views you can do. Have you ever realized the "Per-location view settings" on your list settings page. With this link you can also customize the view for each folder induvidually or per content type like shown in this blog.