2010

A Duplicate Content Type Found Error

Posted by mjimison on June 21, 2011
2010, Content Type, PowerShell, SharePoint / 5 Comments

A duplicate content type “Enter Name” was found

If you’ve gotten this error and come to this site looking for answers, chances are it isn’t simply because the name you’re trying to give a new content type is already in use. On the contrary, you are probably sitting there saying that you know without a doubt the name you are entering is not in use, and you have probably even pounded a random value out on your keyboard just to prove the point.

Maybe you’ve come across forum posts like the following that suggest you make an update directly to the content database (MSDN Forum Post). I never go this route, because it is going to take you out of support with Microsoft, since working with the database is specifically prohibited.

I have, however, created a way to overcome this error utilizing perfectly legal methods.

What Does This Error Mean?

There are 2 ways to create an ID for a content type (MSDN Site). One involves assigning a child id based off a numerical sequence, and the other involves utilizing a unique guid. If you are developing your own content types, I highly recommend always creating your id’s with the unique method because you don’t risk conflicting with another developer or a 3rd party solution, but if you are creating content types through the UI, you have no choice – the UI method always creates children id’s based off of a numerical sequence (until it runs out of numerical values).

Another new feature in SharePoint 2010 also seems to add to this problem, as when utilizing a Content Type Hub, you essentially end up with 2 different sources (the hub and the site) trying to issue children content types and keep track of what the next numerical id should be. The content database has a field on the dbo.ContentTypes table called “NextChildByte” and that is what lets SharePoint know what that next numerical value should be. This is the heart of our problem. Sometimes SharePoint gets out of sync with itself, and “NextChildByte” indicates a value that already is in use. SharePoint is not intelligent enough to continue looking sequentially until it finds the next available byte sequence. Instead, it throws the error you see at the top of the page. That is why the posts online about updating this value in the database work – because the heart of the problem is this value, which is incorrectly telling SharePoint what the next numerical value for a child of this content type should be.

The Solution

I looked at this solution from a few perspectives, but ultimately knew I needed to get this “NextChildByte” value to change without issuing a command on the database. I discovered through testing that every time a content type is created programmatically (where you set the ID – either in the numerical format OR the unique guid format) this value gets incremented by 1. This is an increment of 1 each time – it doesn’t recalculate this value based on the content types already in the site. Essentially what we need to do is create enough “dummy” content types to increment this value up to a point where those numerical values have not yet been used in other content types.

I’ve written a PowerShell function that takes in an SPWeb and SPContentType that you are having trouble creating child content types of through the UI, and it simply adds 10 “dummy” child content types with random id’s and then removes them (thereby also freeing the random id’s it added). This ends up incrementing “NextChildByte” in the database by 10 and fixing your issue. If incrementing by 10 isn’t enough for you, or you run into the problem again down the road, you can update the script to suit your needs and re-run it. You may be wondering what happens after you reach the end of the numerical range expressed by a hexadecimal value (FF). At the point you run off the track (which would seem unlikely) SharePoint is intelligent enough to just create the children with Unique ID’s after that point. “NextChildByte” in the database then sits at 0, even as you add more content types.

function FixContentTypeForChildCreation
{
    param(
    [Microsoft.SharePoint.SPWeb]$web,
    [Microsoft.SharePoint.SPContentType]$ct
    )
 
    #Grab the ID of the CT you need to fix for child creation
    $ctID = $ct.ID.ToString();
 
    for($i=0;$i -lt 10; $i++)
    {
        #Create a new child content type with a unique guid and then delete it
        $newCTID = new-object Microsoft.SharePoint.SPContentTypeId($ctID + "00" + [Guid]::NewGuid().ToString("n"));
        $newCT = new-object Microsoft.SharePoint.SPContentType($newCTID, $web.ContentTypes, "Temp Content Type - Auto Deleted");
        $addedCT = $web.ContentTypes.Add($newCT);
        $newCT.Delete();    
    }
 
    Write-Host "Content Type Fix for Child Creation is Complete"
}

Example Usage

$web = get-spweb "http://yoursite"
$ct = $web.AvailableContentTypes["YourContentType"]
FixContentTypeForChildCreation $web $ct

I hope that this solution is able to help you through what can certainly be a frustrating time for users who are unable to create content types in the UI.

Cheers,
Matt

Share this post:

  • email
  • Twitter
  • Facebook
  • del.icio.us
  • Digg
  • Google Bookmarks
  • StumbleUpon

SharePoint 2010 WebPart Enhancements

Posted by mjimison on October 23, 2009
2010, SharePoint, WebPart / 2 Comments

It’s been too long since my last post. I struggle as a blogger because I feel my time is better spent reading bloggers like Andrew Connell, Waldek Mastykarz, Eric Shupps, and so on, rather than writing my own content. I never imagined this long of a gap between my last posts, but the recent SharePoint conference has re-inspired me to begin writing again.

I was one of the lucky few who was registered and set to hit Vegas for the 2009 SharePoint conference. However, as luck would have it, the H1N1 virus had other plans for me, and I was forced to stay at home and watch all of the conference online. Although I missed out on a lot of the fun, I was able to soak in a lot of the information, and so I want to summarize some of the new features for anyone who wasn’t able to attend, or didn’t get to all of the sessions they had hope to watch.

Most of the content in this post came from a great session on Advanced WebPart development, which was done by Maurice Prather, from ShareSquared. Here are the main points I’d like to highlight:

Versioning

Anyone who has had an author put a lot of content into a Content Editor WebPart, and ask the question “Why is my content not changing when I restore a version?” is going to be happy about this addition. WebParts are now versioned with pages, so you no longer are only left with the option of putting your content into the publishing content fields. There are a lot of  strong opinions out there saying the content editor WebPart shouldn’t even be used, but if you’re in an organization who has decided to continue using it, being able to version that data will be a big help. It will also be nice for keeping configuration setups for other WebParts such as the content query WebPart, where you’ve gone through revisions and want to roll back to earlier settings.

Visual WebParts

This is really not something new to SharePoint 2010. What’s new is Visual Studio’s excellent automation of a system many have been using for a long time. ASP.Net developers are often more comfortable when working within a designer GUI where controls can be dragged and dropped. While using CreateChildControls is perfectly valid, and still a good way to go, creating an experience where less code is written often leads to more productivity. The trick has always been the following:

  1. Create a User Control within Visual Studio
  2. Push the User Control into the ControlTemplates directory inside the 12 Hive (located at: 12 Hive/Templates/ControlTemplates)
  3. Create a WebPart class and use Page.LoadControl inside CreateChildControls to load in your User Control

This same concept is exactly what Visual Studio is now doing for you, but its taking out many of the steps you would perform manually, and making it a much more complete experience.
(Note: Visual WebParts will not work when part of a Sandboxed Solution, something i plan to recap in another post)

Cross-Site Scripting Safeguards (XSS)

This affects old and new WebParts, and it’s important to understand how your current solutions could be affected, even if they will still otherwise work in SharePoint 2010. Simple properties on a WebPart class are normally displayed in the Editor Toolbar (in the default Miscellaneous category if not specified). Many times a developer might take the value of a property entered in the editor, and directly output it to the screen (i.e. new LiteralControl(QuoteOfTheDay) ). The issue with this is that if any html, script, etc… were injected into this property, by outputting it to the screen, malicious code could be run that affects all users to the site. Properties now have a new RequiresDesignerPermission attribute to determine if a property needs designer permissions in order to display on the editor toolbar, and SafeControl entries now have a new SafeAgainstScript attribute to control whether a control is protected. The default behavior without specifying either is that only designers can edit the properties. When SafeAgainstScript is set to true, and RequiresDesignerPermission attribute is set to false, then and only then, can Contributors gain access to edit the property. Going forward this should help to ensure developers are properly encoding values, etc…, before they mark a control as SafeAgainstScript and set RequiresDesignerPermission to false, if they are going to be outputting properties onto the screen.

(Note: personalized properties will not be affected by this new addition, since they would only display for an individual user, and would not be a risk to other viewers)

Miscellaneous

Here are a few things to look forward that don’t necessarily warrant their own section, but are nonetheless interesting:

  • The ASP.Net WebPart class is still the preferred class to inherit over the SharePoint WebPart class
  • More functionality is being added to the ASP.Net WebPart class
  • Developers are encouraged to deploy WebParts as features, and not directly through the solution manifest file or other means
  • Thew new enhanced WIKI allows WebParts to be dropped into anywhere inside the main content area, without having to put it into a WebPartZone (This is all happening behind-the-scenes through a dynamic zone that pushes WebParts into placeholders)

Summary

I hope this has been informative to some of you as to what we have to look forward to in WebParts inside SharePoint 2010. I am sure as Beta is released in November, and the product gets to release, there will be even more  gems to discover inside this new and very promising effort from Microsoft.

Cheers,
Matt

Share this post:

  • email
  • Twitter
  • Facebook
  • del.icio.us
  • Digg
  • Google Bookmarks
  • StumbleUpon