Saturday, June 13, 2009

Adding Documents in document library with feature

After creating document library as feature people are asking how to add document in document library as feature.

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

So here is another simple solution for that.
Below is the feature file

<Feature
Id="BB964AFE-14E1-4f83-9703-73349007D904"
Title="Doclib with document"
Scope="Web"
Version="1.0.0.0"
Hidden="FALSE"
DefaultResourceFile="core"
xmlns="http://schemas.microsoft.com/sharepoint/"
Description="This Feature contains the doc lib with doc">
<ElementManifests>
<ElementManifest Location="element.xml" />
<ElementFile Location="sample\MyDoc1.doc" />
<ElementFile Location="sample\MyDoc2.doc" />
</ElementManifests>
</Feature>



Please note that we have to define our files that we want to put in document library.

This location is path of file from feature.xml.

Below is element.xml

<Elements
xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="withDoc"
Url="MyDoc"
Path="Sample"
RootWebOnly="FALSE">
<File Url="MyDoc1.doc" Type="GhostableInLibrary" />
<File Url="MyDoc2.doc" Type="GhostableInLibrary" />
</Module>
</Elements>


Here is the Description for each element.

Module is the most key element for this thing.

Name: name of the module (any) I have used same as feature name for understanding.

URL: Url of document library from site. If you want document in root level of document library then write only document library name over here. If you want document in some folder of document library then user “<Document Library Name>/<Folder Name>/<Folder Name>”

Path: physical path for document folder (files must have in some folder in feature folder) from feature.xml

RootWebOnly: weather allow this feature only at root level or you want this feature to be at sub site level. False will allow you to use this feature to sub site level also.

<File >
Tag


URL = path of the document after path of module.

Type = GhostableInLibrary : as we want the document to be in document library we have to use this tag.

This way you can add documents in document library.

If you want files to be in different folders then you have to use more than one module tag for diff folders.

Below is element for copying one doc in one library and another doc in another library.

<Elements
xmlns="http://schemas.microsoft.com/sharepoint/">
<Module Name="withDoc"
Url="MyDoc/f1"
Path="Sample"
RootWebOnly="FALSE">
<File Url="MyDoc1.doc" Type="GhostableInLibrary" />
</Module>
<Module Name="withDoc2"
Url="MyDoc/f2"
Path="Sample"
RootWebOnly="FALSE">
<File Url="MyDoc2.doc" Type="GhostableInLibrary" />
</Module>
</Elements>


This step you can use for everything like putting any page in pages library, putting master page in master page library and all that.

6 comments:

Unknown said...

How to fetch the values from OWSTIMER.exe.config?Thanks.

SharePoint Kings said...

Mingle, not getting you question.
Please explain.

frevd said...

thanks, was helpful.

frevd said...

i wasn't able to put this into the same feature as the library, since this would only activate a template, not a list instance, hence the target list does not exist when the file shall be provisioned, so i had to make two separate features. maybe there is a better way.

to deploy with Vse, I had to add a an empty Path-attribute to the File element.

I recognized that it is possible to put the folder path into the File-Url rather than the module so you can have one module and several files in different folder that are being created on demand.

It should also be possible to have webparts instanciated in the file using ..., nice thing to do.

I'm also researching for not ghosting the file, in order to be able to update it with feature updates.

I use the following definition (sample):





thanks again.

SharePoint Kings said...

frevd,

it looks like you XML can not be rendered.

you can use "&lt ; " without space instade of "<" and "&gt ;" without space instade of ">"

Manesh Golekar said...

Thanks..Great Feature.
It saved my Time.




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