Programmatically uploading an attachment to a list item in WSS3/MOSS 2007

If you need to upload a file into a SharePoint document library through code you can get started with this MSDN article: How to: Upload a File to a SharePoint Site from a Local Folder.

In case you need to do the upload the file as an attachment to a custom list using the object model, the approach is slightly different. Adding a file to the list item can be done by accessing the Attachments collection of the SPListIem:

//code snippet
SPList list = web.Lists[
new Guid("my list id")];
if (list != null
)
{
  web.AllowUnsafeUpdates =
true
;
  SPListItem item = list.Items.Add();
  item["Title"] = "my title";

 if (fileAttachment.PostedFile != null && fileAttachment.HasFile)
  {
    
Stream
fStream = fileAttachment.PostedFile.InputStream;

     byte[] contents = new byte[fStream.Length];
     fStream.Read(contents, 0, (
int
)fStream.Length);
     fStream.Close();
     fStream.Dispose();

     SPAttachmentCollection attachments = item.Attachments;
    
string fileName = Path.GetFileName(fileAttachment.PostedFile.FileName);
    
attachments.Add(fileName, contents);

   }

  item.Update();
  web.AllowUnsafeUpdates =
false;

}

//snippet end

 


Running SPSecurity.RunWithElevatedPrivileges in WSS3

When developing a web part or a custom control in WSS/Sharepoint 2007 you might sometimes need to execute some code for which you need more permissions than the one your current user has.
I have this regularly when creating custom controls for Internet Publishing sites in which the anonymous user is the one visiting the site. Say for example that a form is presented to the user that should create new items in a list. An anonymous user does not have a create permission for the list (and we don't want to give the right either).

This can be solved by impersonating the Sharepoint\system user by using the SPSecurity.RunWithElevatedPrivileges method.
MSDN documentation for the method: SPSecurity.RunWithElevatedPrivileges Method (Microsoft.SharePoint).

I ran into a small problem with this method by using it incorrectly… yes my own fault of course but I thought to post an item on this.

Instead of following the example on the MSDN documentation I was using the current context to get an SPWeb object. This does not work because the context has already loaded with the current (anonymous) user’s credentials:

SPSecurity.RunWithElevatedPrivileges(delegate() {
 using (SPSite site = SPControl.GetContextSite(this.Context))
 {

 //implementation here

 }
});

So, always use the web’s ID or URL to load the SPWeb of SPSite object and it works. Just as the documentation shows:

 

SPSecurity.RunWithElevatedPrivileges(delegate()
{
 using (SPSite site = new SPSite(web.Site.ID))
 {

 // implementation details omitted

 }
});

 

Technorati tags: , ,

 


Workflow development in WSS3 and SharePoint 2007 : resources and links

This week I’m starting with the development of my first workflow in MOSS 2007. It’s an extensive topic for which I’m reading as much information as possible. As I’m gathering links and resources I’m posting some of the links I’ve found so far.

Development tools: first things first. In order to get started with custom workflow development:

WSS3 SDK: this SDK also contains the “Workflow Developer Starter Kit for Windows SharePoint Services 3.0” which installs 2 Visual studio templates for workflow:
- Sequential Workflow Library
- State Machine Workflow Library
Installing the SDK also adds the Sharepoint specific workflow activities to the Visual Studio toolbox.

ECM Starter Kit: contains 13 sample workflows using InfoPath forms and 1 sample using custom ASPX pages: http://www.microsoft.com/downloads/details.aspx?familyid=6D94E307-67D9-41AC-B2D6-0074D6286FA9&displaylang=en
These samples are a great way to start learning about how to develop workflows.

Visual Studio 2005 extensions for .NET Framework 3.0 (Windows Workflow Foundation)

Some links:

MSDN: Workflow Configuration Schema Overview (<workflow> schema element) 

Workflow related articles (amongst which a series of 7 posts) on the Microsoft SharePoint Products and Technologies Team Blog

SharePoint 2007 and Windows WorkFlow Foundation: Integrating Divergent Worlds: article by Gustavo Velez. Guides you through the basic process of building a workflow for MOSS using Visual Studio.


Technorati tags: , ,

Copyright © 2007 Katrien De Graeve.