Upload documents to document library in MOSS from a asp.net web site and change Content Type.
I wasworking on a requirement that lets the users upload documents to a MOSS document library from a asp.net site. The web site is installed on a seperate server from the MOSS server. To acheive this, i am using the http:///_vti_bin /copy.asmx web service to upload the file. There is a custom workflow attached to document library that picks the documents and processes them. The problem that i faced was that i needed to be able to change the content type of the document(the document library has multiple content types) before it is processed by the workflow. After finding out how easy it is, i was kicking myself(rightly so) for missing such an easy thing.
Basically the ContentType field is of Type "Choice", so you have to add a field of type "Choice" in your code and your are done. What is strange is that quite a number of people do not know about this and propose doing this using FrontPage RPC calls (ouch..in terms of the effort) or updating the content type after the document has been uploaded(which does not really helps in my scenario). Below is the complete code
//Copy WebService Settings
string webUrl = http://myurl/;
UploadDocuments.Copy copyService = new UploadDocuments.Copy();
copyService.Url = webUrl + "/_vti_bin/copy.asmx";
//put in the credentials that have the permissions to insert a document in the document library.
NetworkCredential credentials = new NetworkCredential("test", "test", "test");
copyService.Credentials = credentials;
//Declare and initiates the Copy WebService members for uploading
string sourceUrl = "C:\\SampleDocument.txt"; //source of the document
//the location that the document is going. the last part "SampleDocument.txt" needs to be replaced with
//the actual filename.
string[] destinationUrl = { "http://idx1/Drop%20Folder/SampleDocument1.txt" };
UploadDocuments.CopyResult cResult1 = new UploadDocuments.CopyResult();
UploadDocuments.CopyResult cResult2 = new UploadDocuments.CopyResult();
UploadDocuments.CopyResult[] cResultArray = { cResult1, cResult2 };
//the meta data that is going to be populated.
UploadDocuments.FieldInformation fFiledInfo = new UploadDocuments.FieldInformation();
fFiledInfo.DisplayName = "Document Name";
fFiledInfo.Type = UploadDocuments.FieldType.Text;
fFiledInfo.Value = "Sample Description";
UploadDocuments.FieldInformation fFiledInfo1 = new UploadDocuments.FieldInformation();
fFiledInfo1.DisplayName = "Document Location";
fFiledInfo1.Type = UploadDocuments.FieldType.Text;
fFiledInfo1.Value = "my location";
UploadDocuments.FieldInformation fFiledInfo2 = new UploadDocuments.FieldInformation();
fFiledInfo2.DisplayName = "Content Type";
fFiledInfo2.InternalName = "ContentType";
fFiledInfo2.Type = UploadDocuments.FieldType.Choice;
fFiledInfo2.Value = "myContentType";
UploadDocuments.FieldInformation fFiledInfo3 = new UploadDocuments.FieldInformation();
fFiledInfo3.DisplayName = "Column1";
fFiledInfo3.Type = UploadDocuments.FieldType.Text;
fFiledInfo3.Value = "test";
UploadDocuments.FieldInformation fFiledInfo4 = new UploadDocuments.FieldInformation();
fFiledInfo4.DisplayName = " Number";
fFiledInfo4.Type = UploadDocuments.FieldType.Text;
fFiledInfo4.Value = "Somevalue";
UploadDocuments.FieldInformation fFiledInfo5 = new UploadDocuments.FieldInformation();
fFiledInfo5.DisplayName = "First Name";
fFiledInfo5.Type = UploadDocuments.FieldType.Text;
fFiledInfo5.Value = "First Name";
UploadDocuments.FieldInformation fFiledInfo6 = new UploadDocuments.FieldInformation();
fFiledInfo6.DisplayName = "Last Name";
fFiledInfo6.Type = UploadDocuments.FieldType.Text;
fFiledInfo6.Value = " Last Name";
UploadDocuments.FieldInformation[] fFiledInfoArray = { fFiledInfo, fFiledInfo1 , fFiledInfo2, fFiledInfo3, fFiledInfo4, fFiledInfo5, fFiledInfo6 };
//Reading the document contents in to stream
FileStream strm = new FileStream(sourceUrl, FileMode.Open, FileAccess.Read);
byte[] fileContents = new Byte[strm.Length];
byte[] r = new Byte[strm.Length];
int ia = strm.Read(fileContents, 0, Convert.ToInt32(strm.Length));
strm.Close();
//Copy the document from SourceUrl to destinationUrl with metadata
//in the actual application, you will need to parse the cResultArray to find out if the
//copy to succedded.
uint copyresult = copyService.CopyIntoItems(sourceUrl, destinationUrl, fFiledInfoArray, fileContents, out cResultArray);
Basically the ContentType field is of Type "Choice", so you have to add a field of type "Choice" in your code and your are done. What is strange is that quite a number of people do not know about this and propose doing this using FrontPage RPC calls (ouch..in terms of the effort) or updating the content type after the document has been uploaded(which does not really helps in my scenario). Below is the complete code
//Copy WebService Settings
string webUrl = http://myurl/;
UploadDocuments.Copy copyService = new UploadDocuments.Copy();
copyService.Url = webUrl + "/_vti_bin/copy.asmx";
//put in the credentials that have the permissions to insert a document in the document library.
NetworkCredential credentials = new NetworkCredential("test", "test", "test");
copyService.Credentials = credentials;
//Declare and initiates the Copy WebService members for uploading
string sourceUrl = "C:\\SampleDocument.txt"; //source of the document
//the location that the document is going. the last part "SampleDocument.txt" needs to be replaced with
//the actual filename.
string[] destinationUrl = { "http://idx1/Drop%20Folder/SampleDocument1.txt" };
UploadDocuments.CopyResult cResult1 = new UploadDocuments.CopyResult();
UploadDocuments.CopyResult cResult2 = new UploadDocuments.CopyResult();
UploadDocuments.CopyResult[] cResultArray = { cResult1, cResult2 };
//the meta data that is going to be populated.
UploadDocuments.FieldInformation fFiledInfo = new UploadDocuments.FieldInformation();
fFiledInfo.DisplayName = "Document Name";
fFiledInfo.Type = UploadDocuments.FieldType.Text;
fFiledInfo.Value = "Sample Description";
UploadDocuments.FieldInformation fFiledInfo1 = new UploadDocuments.FieldInformation();
fFiledInfo1.DisplayName = "Document Location";
fFiledInfo1.Type = UploadDocuments.FieldType.Text;
fFiledInfo1.Value = "my location";
UploadDocuments.FieldInformation fFiledInfo2 = new UploadDocuments.FieldInformation();
fFiledInfo2.DisplayName = "Content Type";
fFiledInfo2.InternalName = "ContentType";
fFiledInfo2.Type = UploadDocuments.FieldType.Choice;
fFiledInfo2.Value = "myContentType";
UploadDocuments.FieldInformation fFiledInfo3 = new UploadDocuments.FieldInformation();
fFiledInfo3.DisplayName = "Column1";
fFiledInfo3.Type = UploadDocuments.FieldType.Text;
fFiledInfo3.Value = "test";
UploadDocuments.FieldInformation fFiledInfo4 = new UploadDocuments.FieldInformation();
fFiledInfo4.DisplayName = " Number";
fFiledInfo4.Type = UploadDocuments.FieldType.Text;
fFiledInfo4.Value = "Somevalue";
UploadDocuments.FieldInformation fFiledInfo5 = new UploadDocuments.FieldInformation();
fFiledInfo5.DisplayName = "First Name";
fFiledInfo5.Type = UploadDocuments.FieldType.Text;
fFiledInfo5.Value = "First Name";
UploadDocuments.FieldInformation fFiledInfo6 = new UploadDocuments.FieldInformation();
fFiledInfo6.DisplayName = "Last Name";
fFiledInfo6.Type = UploadDocuments.FieldType.Text;
fFiledInfo6.Value = " Last Name";
UploadDocuments.FieldInformation[] fFiledInfoArray = { fFiledInfo, fFiledInfo1 , fFiledInfo2, fFiledInfo3, fFiledInfo4, fFiledInfo5, fFiledInfo6 };
//Reading the document contents in to stream
FileStream strm = new FileStream(sourceUrl, FileMode.Open, FileAccess.Read);
byte[] fileContents = new Byte[strm.Length];
byte[] r = new Byte[strm.Length];
int ia = strm.Read(fileContents, 0, Convert.ToInt32(strm.Length));
strm.Close();
//Copy the document from SourceUrl to destinationUrl with metadata
//in the actual application, you will need to parse the cResultArray to find out if the
//copy to succedded.
uint copyresult = copyService.CopyIntoItems(sourceUrl, destinationUrl, fFiledInfoArray, fileContents, out cResultArray);
Comments
Post a Comment