Sunday, February 1, 2009

Upload a document in DOCUMENT LIBRARY in WSS 3.0 programatically.

As I mentioned there are two ways to interact with WSS 3.0
1)WebService Approach
2)Object model approach.

So below is the code you have to do while following the service approach to upload a document programatically.


[WebMethod]
public bool UploadTemplate(Byte[] fileInBytes, string FileName, string MetaInformation)
{
try
{
NetworkCredential objCredential = new NetworkCredential();
objCredential.Domain = ConfigurationSettings.AppSettings["NetworkDomain"];
objCredential.UserName = ConfigurationSettings.AppSettings["NetworkUserName"];
objCredential.Password = ConfigurationSettings.AppSettings["NetworkPassword"];

string[] destURI = { ConfigWrapper.WebSiteUrl + ConfigWrapper.DoucmentDirectory + "/" + FileName };

Copy webCopyObj = new Copy();
webCopyObj.Credentials = objCredential;
webCopyObj.Url = ConfigWrapper.CopyServiceUrl;

FieldInformation myFieldInfo = new FieldInformation();
myFieldInfo.DisplayName = "MetaInformation";
myFieldInfo.Value = MetaInformation;
myFieldInfo.InternalName = "MetaInformation";
myFieldInfo.Type = FieldType.Text;
FieldInformation[] myFieldInfoArray = { myFieldInfo };

CopyResult myCopyResult2 = new CopyResult();
CopyResult[] myCopyResultArray = { myCopyResult2 };

CopyResult[] result;

uint documentId = webCopyObj.CopyIntoItems("http://null", destURI, myFieldInfoArray.ToArray(), fileInBytes, out result);
byte[] temp;
uint tempId = webCopyObj.GetItem(destURI[0], out myFieldInfoArray, out temp);
Guid myItemGUID = myFieldInfoArray[0].Id;
return true;
}
catch
{
return false;
}
}



webCopyObj will be the object of the copy web service taken from the site.So for example you have to include the web reference from http://localhost:15182/_vti_bin/Copy.asmx.which referes to /_vti_bin/Copy.asmx.


Will write about the second approach (Object model approach) later.


ConfigWrapper is the object of the class written for fetcing the information from appSetting section from web.config