Code for Creating New Documents based on a content type and template
This method “CreateDocument” will create a new document in a SharePoint document library using a content type. It will copy the document template associated with the content type into the new document:
// Creates document in given list (root folder).
// Returns true if the file was created, false if it already
// exists or throws exception for other failure
protected bool CreateDocument( string sFilename,
string sContentType, string sList)
{
try
{
SPSite site = SPContext.Current.Site;
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists[sList];
// this always uses root folder
SPFolder folder = web.Folders[sList];
SPFileCollection fcol = folder.Files;
// find the template url and open
string sTemplate =
list.ContentTypes[sContentType].DocumentTemplateUrl;
SPFile spf = web.GetFile(sTemplate);
byte[] binFile = spf.OpenBinary();
// Url for file to be created
string destFile = fcol.Folder.Url + “/” + sFilename;
// create the document and get SPFile/SPItem for
// new document
SPFile addedFile = fcol.Add(destFile, binFile, false);
SPItem newItem = addedFile.Item;
newItem["ContentType"] = sContentType;
newItem.Update();
addedFile.Update();
return true;
}
}
catch (SPException spEx)
{
// file already exists?
if (spEx.ErrorCode == -2130575257)
return false;
else
throw spEx;
}
}
This code:
- Gets a SPSite for the current site collection, and opens an SPWeb for the site.
- Gets a SPList for the given list, and then an SPFolder for the root folder in this list. This code always creates the document in the root folder, but the code can easily be changed to place the document in any folder in the document library.
- Gets a SPFileCollection for the documents in the folder.
- “DocumentTemplateUrl” is used to return the Url of document template associated with the given content type.
- Get an SPFile for the document template in spf and open it for binary access using OpenBinary
- Add a new document to the folder through the SPFileCollection referenced by fcol.
- Get an SPItem for the new document and set the “ContentType” column to ensure it uses the correct content type (it will default to the first content type in the document library).
- Update the item and the added file.
- The catch section checks for an -2130575257 error, which indicates the file already exists.
Nice article. I was looking for the exact code implementation on the net when I found your article.
Can you also provide inputs on how to add values to certain fields in the document before uploading the file.
The document here refers to the document based on the template/ content type.
Swati Agarwal
January 2, 2010 at 9:52 am
If template is xyz.xsn — infopath template file
Then what would be the aditional steps to create infopath .xml file document?
Thanks
Swati Agarwal
January 2, 2010 at 3:59 pm
Hi, I am looking for the same scenario…
the only difference is, we are using SharePoint Client Object Model for .net Managed Windows Application.
Will you please let me know how the same above thing possible in client object model also ?
Thanx,
Jinesh
Jinesh
January 24, 2012 at 11:14 am