Document Library : Add through RPC Call

Here is the code snippet which uses RPC call to upload document to a SharePoint Document Library

private
void
uploadDocument()
{
Stream io_stream;
MemoryStream stream;
byte[] contents;
string strFileName = string.Empty;string
strPostBody =
string.Empty;
bool goAhead = false;

//CURRENT SITE URL

string strCURRENT_SITE_URL = Configuration.getCurrentSiteURL();
//Calculating File Name
strFileName = fileupload.PostedFile.FileName;
if
(strFileName.IndexOf("\\")!= -1)
{
strFileName = strFileName.Substring(strFileName.LastIndexOf(
"\\")+1);
}
strFileName = strFileName.Substring(strFileName.LastIndexOf(
"\\")+ 1);

//CREATE BOSY STRING FORRPC CALL

strPostBody = GetPostBodyStringWithOutMetaInfo(strCURRENT_SITE_URL,strFileName,
string.Empty);
//START UPLOADINGDOCUMENT

stream =
new MemoryStream();
io_stream = fileupload.PostedFile.InputStream;
contents =
new byte[io_stream.Length];
io_stream.Read(contents, 0, (
int)io_stream.Length);
io_stream.Close();
ASCIIEncoding encoding =
new ASCIIEncoding();
stream.Write(encoding.GetBytes(strPostBody), 0,strPostBody.Length);
stream.Write(contents,0,contents.Length);strPostBody = SendRequest(strCURRENT_SITE_URL +

"/_vti_bin/_vti_aut/author.dll",
stream.GetBuffer(), stream.Length);
stream.Close();

//END OF UPLOADINGDOCUMENT


//GET DOCUMENT ID USING QUERY FOR ADDING DOCUMENT TO REFERENCE LISTS

SPQuery query =
new SPQuery();
int
docID;
query.Query =
"<Where><Eq><FieldRefName='FileLeafRef' /><Value Type='File'>" + strFileName +"</Value></Eq></Where>";

SPListItemCollection listItemCollection = web.Lists[GreenstoneListNames.GREENSTONE_DOCUMENT_LIBRARY].GetItems(query);

if(listItemCollection.Count> 0)
{
docID = listItemCollection[0].ID;
}

}

I hope you find it .

No comments:

Post a Comment