Adding Custom Folder ContentType Item to a Doc Library

Hey Guys,
Today I ran into an really stupid error. I have an Doc Library in a SharePoint Site, I create folders programmatically. Now I had the requirement to add MetaTags to that Library. After I added custom Columns to that Library my Code to Create a Folder in that Library won't work anymore, because the SPFolder.Item Object has been null.

After a while of searching I found this post, and the explanation why this happens!?

So how could I add a Folder to a Library with MetaTags!?
First of all create a new ContentType of Type Folder and insert all required MetaTags (see my post Create Folder with MetaTags)
After you have done this, you must change the code for creation of the Folder.

SPDocumentLibrary doclib = (SPDocumentLibrary)(m_SharePointWeb.GetListsOfType(SPBaseType.DocumentLibrary)[libraryName]);
string url = ((SPFolder)(doclib.RootFolder)).ServerRelativeUrl.ToString();
SPFolder folder = web.GetFolder(url);

web.AllowUnsafeUpdates = true;

SPListItem newItem = doclib.Items.Add(
doclib.RootFolder.ServerRelativeUrl,
SPFileSystemObjectType.Folder,
null);
newItem["Title"] = folderName;
newItem["Name"] = folderName;
newItem["ContentType"] = folderType;

newItem.Update();

doclib.RootFolder.Update();
web.Update();


Posted May 05 2009, 12:07 PM by Nadine Storandt

Comments

together with wrote re: Adding Custom Folder ContentType Item to a Doc Library
on 12-03-2012 15:13

Usually I do not learn article on blogs, but I wish to say that this write-up very forced me to check out and do so! Your writing style has been surprised me. Thanks, very nice post.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
developers.de is a .Net Community Blog powered by daenet GmbH.