Friday, May 16, 2008

How to add document library with folders and content types through feature/site definition.

When creating custom features, definitions one of the things most clients
want is a document library included in the Site Template with a
pre-defined folder structure.


In this post I’m going to narrate how to provision document library with folders form a custom feature.

So here we go.

Open element.xml of your feature it should be something like this.

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<ListInstance
FeatureId="00BFEA71-E717-4E80-AA17-D0C71B360101"
TemplateType="101"
Title="My Docs"
Url="My Docs"
Description=""
QuickLaunchUrl="My Docs/Forms/AllItems.aspx" />
</Elements>



Over here we are just creating an instance of a document library.
In SharePoint by default you will find a feature with forlder name “DocumentLibrary” in your 12/template/feature. So we do not need to create whole schema file just like custom list.







Creating a list instance with this
GUID is same as creating a document library from

Site Settings > Create > Document Library.




Description of each property

FeatureId --> Hardcode guid which refers SharePoint Document Library.
TemplateType --> 101 denotes that we want to create Document library. 100 for list and 115
for content type etc...
Title --> My Docs simple title of newly created List.
Url --> My Docs same as Title. You can use different if you want but same will be better to maintain and understand.
Description --> just description non mandatory.
QuickLaunchUrl --> My Docs/Forms/AllItems.aspx Just like /Forms/AllItems.aspx, use you used in url tab.

If you installed feature with this element TAG you will get Document library.

Now here we go for folder.
Add the following code just before the </Elements> tag:

<Module Path="First Folder" Url="My Docs \First Folder” />

Note : The Url in the above tag begins with the url of the list instance.

If you want to add sub folders to the above folder

<Module Path="First Folder" Url="My Docs \First Folder\sub folder of first folder” />


Note : The ListUrl in the above tag begins with the url of the list instance.

If you want to add content type to the folder then you need to add one more tag

<ContentTypeBinding ListUrl="My Docs" ContentTypeId="0x010110" />

Note : you already have a contentype installed with specific ContentTypeId.

My next Post will be: How to create/install Contentype from feature. Comming Soon.

You can create multiple document libraries from one feature.
Just add more than one List instance.
Here is one sample of my element.xml

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- Doc lib 1-->
<ListInstance
FeatureId="00BFEA71-E717-4E80-AA17-D0C71B360101"
TemplateType="101"
Title="My Docs"
Url="My Docs"
Description=""
QuickLaunchUrl="My
Docs/Forms/AllItems.aspx" />

<!-- required custom content type for this document library instance -->

<ContentTypeBinding ListUrl="My Docs"
ContentTypeId="0x010100A107D9F002764eea923263A0B3587210" />
<!-- Single folder no sub-folders -->

<Module Path="Administration" Url="My Docs/Administration" />

<!-- A Folder and associated sub-folders -->
<Module Path="Development" Url="My Docs/Development" />
<Module Path="Development" Url="My Docs/Development/Code Review" />
<Module Path="Development" Url="My Docs/Development/PreRequisite" />
<!-- Doc lib 2-->
<ListInstance
FeatureId="00BFEA71-E717-4E80-AA17-D0C71B360101"
TemplateType="101"
Title="Your Docs"
Url="Your Docs"
Description=""
QuickLaunchUrl="Your Docs/Forms/AllItems.aspx" />

<ContentTypeBinding ListUrl="Your Docs" ContentTypeId="0x010100D8B76062F25E42efB17D4CBF9747FB57" />
<Module Path="Business Analyst" Url="Your Docs/Business Analyst" />
<Module Path="Functional Analyst" Url="Your Docs/Functional Analyst" />
<Module Path="Functional Analyst" Url="Your Docs/Functional Analyst/Requirment" />
<Module Path="Functional Analyst" Url="Your Docs/Functional Analyst/change Request" />
</Elements>



and speacial thanks to "bobthebuilder" for make me under
stand all this functionality from his wonderful post @ SHAREPOINT.H@CK.





10 comments:

Unknown said...

Hi, I'm trying to create a SharePoint solution where a document library is created and documents are added to it. I would like to know you if it is possible to do the same you do with folders but with documents.
Thanks in advance.

Anonymous said...

Great!
You made me understand with one page, all I was trying to understand in many pages of the MSDN!
AND IT WORK PERFECTLY!

Parth Patel said...

@sebastianm: we just posted answer your query.
please check this post
Adding Documents in document library with feature

Unknown said...

That's great Parth, thank you very much.

Manu said...

Hi,I have created folders inside the document library with your methos.But then the files associated with my content types are not working.

SharePoint Kings said...

Manu,
please check note in the article

"you already have a contentype installed with specific ContentTypeId."

if content type are already installed and your document has already associated with it. then it should appear.

Kevin said...

Hi,

Thanks for the interesting post. I was wondering what files I need apart from the Elements.xml which include the listInstance. I tried to create a feature using Feature.xml and Elements.xml but doesn't work. Can you help me out?

Thanks,
Kevin.

SharePoint Kings said...

Kevin,

Its simple funda,
in feature.xml you have to define files(any number any name)

and feature activation will check the file and do the things you mentioned in that file.

kevin said...

Thanks for the answer. It worked. I have one other question though, is it possible to assign permissions to the folders using the feature?

SharePoint Kings said...

kevin,

Thanks,

you can not direct assign permission to the folder from feature xml only

you have to that from code.
but you can put that code in feature receiver so when your feature activated your code will be executed and from there you can give permission.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com