Tuesday, July 17, 2012

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.

No comments:

Post a Comment