Friday, August 8, 2008

Create new folder, Upload document to sharepoint site – Maintain metadata

There was one requirement which I was working on recently. It was very simple to create a user control wherein the user should be able to select the file and upload it to a WSS 3.0 Document Library. Also the second part was user should be able to create folders in the document library and upload document inside the folder.

 

With this very simple task was stated to me, I was very happy to answer my boss on the spot that the, I have the design ready. But boss (being a boss !) told me, Hold On! The requirement also states that the user should be able to see his/her name after uploading document and creating folder. For this I had a very straight forward solution which is inbuild for WSS 3.0. I used following code to upload the document.

    /// <summary>

/// Uploads Document to the Site

    /// foldername: Name of folder to which the document is to be uploaded. (Eg. "Shared Documents")

    /// documentFIleName: FileName of the file to be uploaded. Typically FileUpload control's output after formatting gets in here (Eg. "C:\MyFile.Doc")

/// </summary>

public void UploadDocumentToSite(string foldername, string documentFileName)

{

SPSite site = SPControl.GetContextSite(HttpContext.Current);

SPWeb myWeb = site.AllWebs["123"]; //123 is the site id.

 

try

{

site.AllowUnsafeUpdates = true;

myWeb.AllowUnsafeUpdates = true;

 

SPFolder destFolder = myWeb.GetFolder(foldername);

SPUser user = myWeb.Users["Domain\\UserName"];

 

destFolder.Files.Add(documentFileName, content, user, user, DateTime.Now, DateTime.Now);

         //The above overload used will help me to maintain the metadata such as created by and modified by fields.

 

}

catch (Exception ex)

{

//Log the error here

}

finally

{

//Dispose the web and site

}

}

 

So far so good. The above code perfectly uploads the document to the document library and also maintains the metadata such as Created by/Modified By fields.

The challenge starts later. Now the same requirement had a provision that user should be able to create folders in the document library. Creating folder is simple

SPFolder destFolder = myWeb.GetFolder(foldername);

destFolder.SubFolders.Add(folderName);

Now the problem starts. The above SubFolders.Add method does not have any overload where I can pass on SPUser. Apparently what would happen is that the document gets uploaded with some other userid (or system account which was impersonated) but not with the specific userid I wanted.

Then I again searched on the web and somewhere down the line I got a hint which worked for me like anything. The below is the way to go about it.

SPFolder newFolder = destFolder.SubFolders.Add(folderName);

newFolder.Item["Modified By"] = user.ID;

newFolder.Item["Created By"] = user.ID;

newFolder.Item.Update();

 

Wow… This was so simple but still so important to know.. I am very thankful to the guy who floated this on the web. (Unfortunately I did not bookmarked the url, but I commented down there)..

Go ahead and try this out if this works for you.. if it does, keep the work spreading…

Enjoy Life .. Enjoy Programming.. ..

Let me know if you need any assistance on such issues…