Thursday, May 28, 2009

Upload Document To MOSS 2007


SPSite siteCollection = null;
SPWeb topLevelSite = null;
string strDocLibraryLink = System.Configuration.ConfigurationManager.AppSettings["SPDocumentLibrary"].ToString();
//"http://#/Corporate/DealDesk/Documents/";
siteCollection = new SPSite(strDocLibraryLink);
topLevelSite = siteCollection.AllWebs[ConfigurationManager.AppSettings["TopLevelSite"].ToString()];
// siteCollection.AllWebs["Corporate/DealDesk"];


topLevelSite.AllowUnsafeUpdates = true;
SPList list = topLevelSite.Lists["Documents"];
//foreach (SPListItem item in list.Items)
//{
// if (Convert.ToString(item["ID"]).Equals(strUARID))
// {
SPFolder objFolder = topLevelSite.GetFolder("documents");
byte[] bufDoc = null;
string strFileName = Path.GetFileName(filename.FileName);
strMossFileName = strFileName;
int nLen = filename.ContentLength;
bufDoc = new byte[nLen];
Stream oStream = filename.InputStream;
oStream.Read(bufDoc, 0, nLen);
// SPFile file = objFolder.Files.Add(strFileName, bufDoc, true);
System.GC.AddMemoryPressure(200024);
SPFile file = objFolder.Files.Add(strFileName, oStream, true);
Response.Write("File Name:" + file);
Response.Write("File ID:" + file.Item["ID"].ToString());
//strFileName = file;
file.Update();


Note: In K2 we should not upload document, tr .net only we have upload and then in k2 we have update Metadata only tr file name as a identity.

No comments: