Nick Grattan's Blog

About Microsoft SharePoint, .NET, Natural Language Processing and Machine Learning

Posts Tagged ‘C#

Code for Creating New Documents based on a content type and template

with 16 comments

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:

  1. Gets a SPSite for the current site collection, and opens an SPWeb for the site.
  2. 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.
  3. Gets a SPFileCollection for the documents in the folder.
  4. “DocumentTemplateUrl” is used to return the Url of document template associated with the given content type.
  5. Get an SPFile for the document template in spf and open it for binary access using OpenBinary
  6. Add a new document to the folder through the SPFileCollection referenced by fcol.
  7. 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).
  8. Update the item and the added file.
  9. The catch section checks for an -2130575257 error, which indicates the file already exists.

 

Advertisements

Written by Nick Grattan

December 8, 2008 at 10:23 pm

Trimming WebPart Output with SPSecurityTrimmedControl

with one comment

The SPSecurityTrimmedControl can be used in your SharePoint pages and web parts to conditionally display content based on permissions assigned to a user.

Here’s an example of using the control in a web part. The control is declared in Microsoft.SharePoint (in microsoft.sharepoint.dll). First, create an instance:

SPSecurityTrimmedControl stcEditItem= new SPSecurityTrimmedControl();
stcEditItem.Permissions = SPBasePermissions.EditListItems;

Next, specify the output to display only if the current user has the given permissions. In this case

  • Define a literal server control, add some content
  • Add the literal to the SPSecurityTrimmedControl
  • Add the SPSecurityTrimmedControl to the controls collection for the web part:

Literal litEditItem = new Literal();
litEditItem.Text = “My Content”;
stcEditItem.Controls.Add(litEditItem);
this.Controls.Add(stcEditItem);

Finally, you can explicitly render the SPSecurityTrimmedControl control in the web part’s “Render” method:

stcEditItem.RenderControl(writer);

The content “My Content” will only be displayed in the web part if the user has the “EditListItems” permission.

Also, you can programmatically test for permissions on various SharePoint objects, such as SPListItem, using the AllRolesForCurrentUser method:

SPListItem li;

if (!li.AllRolesForCurrentUser.Contains(web.RoleDefinitions[“Approve”]))
{
    …

This web post from Richard Harbridge shows how to use the SPSecurityTrimmedControl in an ASPX page.

Written by Nick Grattan

November 13, 2008 at 1:05 pm