Thursday, April 15, 2010

Uploading a document into SharePoint Document Library Part 2

Yes, this is part 2. Uploading a document in a sharepoint document library where the settings for the document libary are

1. Create a version each time you edit a file in this document library? Yes (Create major or minor versions )
2. Require documents to be checked out before they can be edited? Yes

And after uploding the document

1. You want to update a metadata column also for same document.
2. Check the document back in.

Here in this process followings are the main steps

Step 1: Get the file in binary format to be uploaded.
Step 2: Check if the file already exists in the library.
Step 3: Check if file is already checked out by somebody else.
Step 4: If file exists and not checked out by anybody else then check it out. (You wont be able to upload a file if it is not checked out).
Step 5: Upload the file.
Step 6: Check-in the file.



protected void HandleUploadDocument(object sender, EventArgs eventArgs)
{
try
{



bool flgPublish = true;

if (attachdoc.PostedFile != null)
{
if (attachdoc.PostedFile.ContentLength > 0)
{

string filename = attachdoc.PostedFile.FileName.ToString();
string[] split = filename.Split(new Char[] { '\\' });
filename = split[split.Length - 1].ToString();

System.IO.Stream strm = attachdoc.PostedFile.InputStream;
byte[] byt = new byte[Convert.ToInt32(attachdoc.PostedFile.ContentLength)];
strm.Read(byt, 0, Convert.ToInt32(attachdoc.PostedFile.ContentLength));
strm.Close();

// Open site where document library is created.
SPSite objSite = SPContext.Current.Site;
SPWeb objWeb = objSite.OpenWeb();
SPFolder mylibrary = objWeb.Folders["Project Documents"];

if (objWeb.GetFile("Project Documents/" + filename).Exists)
{
if (objWeb.GetFile("Project Documents/" + filename).CheckOutStatus == SPFile.SPCheckOutStatus.None)
{
objWeb.GetFile("Project Documents/" + filename).CheckOut();
}
else
{
flgPublish = false;
SPUser chkBy = objWeb.GetFile("Project Documents/" + filename).CheckedOutBy;
url = "checkedout";
lblLessonListError.Text = "Cannot upload the file. File is checked out by " + chkBy.LoginName;
trLessonListError.Visible = true;
}
}

if (flgPublish == true)
{
objWeb.AllowUnsafeUpdates = true;
SPFile spfile = mylibrary.Files.Add(System.IO.Path.GetFileName(filename), byt, true);

SPDocumentLibrary docs = (SPDocumentLibrary)objWeb.Lists[mylibrary.ContainingDocumentLibrary];
SPListItem item = docs.Items[spfile.UniqueId];
item["Project_x0020_Phase"] = "Metadata Text";
item.Update();

spfile.CheckIn("Checked in", SPCheckinType.MinorCheckIn);
objWeb.AllowUnsafeUpdates = false;
url = spfile.Item["Encoded Absolute URL"].ToString();
}

}
}


}
catch (Exception ex)
{
SW = File.AppendText("D:\\temp\\LogImport.txt");
SW.WriteLine(" Error Occurred : " + ex.Message + " User : " + SPContext.Current.Web.CurrentUser.ToString() + " " + DateTime.Now.ToString() + " " + ex.StackTrace);
SW.WriteLine("--------------------------------------------------");
SW.Close();
}

}



Leave the question, I will respond for any of your query.

Thanks
Sanjay Tiwari
s_tiwari05@yahoo.com

No comments: