Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Server Technologies
SDK Documentation
General Reference
 How to: Provision a File
How to: Provision a File

To provision a file into SharePoint Web sites, you must use the Module element within a Feature or site definition. The Module element allows you to add one or more files, or file set, to a SharePoint Web site or document library.

NoteNote:

Windows SharePoint Services 3.0 supports provisioning a maximum 1000 files through Features, whether it be 1000 modules each with 1 file, or 1 module with 1000 files.

The Module element contains File elements that define which files to provision, and that can also contain definitions for the set of Web Parts to add. If you provision files to a subdirectory through the Module element, Windows SharePoint Services automatically ensures that the directory structure is created to match your files.

Through the Module element you can opt either to provision a file into a document library or to add a normal file (for example, default.aspx) outside a document library. In the case of adding a file to a document library, you must specify Type="GhostableInLibrary" as an attribute in the File element. This setting tells Windows SharePoint Services to create a list item to go with your file when it is added to the library. If you are instead provisioning a file outside a document library, specify Type="Ghostable".

The following example provisions a few new master pages into the master page document library.

Feature.xml

<Feature Id="947F6C32-E898-45f5-A595-C46F07DA6BF7"
  Title="New Master Pages"
  Scope="Web"
  xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="newmasterpages.xml" />
  </ElementManifests>
</Feature>

newmasterpages.xml

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="MPages" List="116" Url="_catalogs/masterpage">
    <File Url="Minimal.master" Type="GhostableInLibrary" />
    <File Url="ContosoRed.master" Type="GhostableInLibrary" />
  </Module>
</Elements>

The following example from a Windows SharePoint Services 2.0 site definition shows how to provision a file through the Onet.xml file. You can differentiate between where the file is located on disk and where it gets provisioned. Note that default.aspx is stored at dws\default.aspx relative to the root of the site definition, but is provisioned to /default.aspx in the site.

The example also shows how to use View elements to include list view Web Parts, as well as arbitrary Web Parts (AllUsersWebPart element) for adding non-list view Web Parts to the page. In addition, the example contains NavBarPage elements, which declare this page to be the home page of the SharePoint Web site.

<Module Name="DWS" Url="" Path="dws">
  <File Url="default.aspx">
    <View List="104" BaseViewID="3" WebPartZoneID="Top"/>
    <View List="103" BaseViewID="3" WebPartZoneID="Right" WebPartOrder="2"/>
    <View List="101" BaseViewID="6" WebPartZoneID="Left">
      <![CDATA[
        <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
          <Title>Members</Title>
        </WebPart>
      ]]>
    </View>
    <View List="107" BaseViewID="7" WebPartZoneID="Left" WebPartOrder="2"/>
    <AllUsersWebPart WebPartZoneID="Right" WebPartOrder="1">
      <![CDATA[
        <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">
          <Assembly>Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
          <TypeName>Microsoft.SharePoint.WebPartPages.MembersWebPart</TypeName>
          <Title>Members</Title>
          <Description>Use the Members Web Part to see a list of the site members and their online status.</Description>
          <FrameType>Standard</FrameType>
          <IsVisible>true</IsVisible>
        </WebPart>
      ]]>
    </AllUsersWebPart>
    <NavBarPage Name="Home" ID="1002" Position="Start">  </NavBarPage>
    <NavBarPage Name="Home" ID="0" Position="Start">  </NavBarPage>
  </File>
</Module>

Provisioning a Folder

Use the Url attribute of the Module element to provision a folder as part of the module. The following example provisions a site with a folder called MyImages that contains a specified image file.

Xml
<Module Name="MyImage" Url="MyImages" Path="">
  <File Url="MyImage.jpg" Type="GhostableInLibrary" />
</Module>

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Correction on the last sample code snippet      JeffLin   |   Edit   |  

The value in the List attribute should be the path to the list and not the list type. Another better example can be found in http://msdn2.microsoft.com/en-us/library/ms460356.aspx .

Tags What's this?: Add a tag
Flag as ContentBug
ElementFile      Jussi Palo   |   Edit   |  
In order to ensure that the feature can be properly upgraded all files belonging to the feature should be included in <ElementFile> tags. If files are not properly defined in <ElementFile> tags they will not always be upgraded when the feature is upgraded.
Tags What's this?: Add a tag
Flag as ContentBug
The last sample will fail.      fallenrogue   |   Edit   |  
< Module Name="MyImage" Url="MyImages" Path="">
<File Url="MyImage.jpg" Type="GhostableInLibrary" />
</Module>

that sample will never work. You can provision a new "folder" through this module in feature deployment but you cannot provision a new Document Library this way. The attribute "Type" with value "GhostableInLibrary" specifies that you want a DocLib created at the url specified which you can't do without getting the following error:

Failed to instantiate file [X] from module [Y] : The specified list does not exist.

Remove the type attribute and you'll find that your files are provisioned correctly to the new folder. Remember, this is not a new list or doclib but a new folder with your contents. Oh, and it's not removed during deactivation from the site. You'll have to perform the cleanup on your own.

I hope that save you all a little time!

~Leon
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker