Technical stuff you probably wouldn't understand.

Rather than cluttering up other blogs with my musings, I made my own.

Wednesday, June 28, 2006

Sharepoint document uploads with support for versioning.

This post is a continuation/addition to the post by Erika Ehrli

I successfully implemented the sample, and with some small modifications came up with an enhancment to allow the document to be uploaded from a stream instead of a file on the file system.

One tidbit that I added to my client app was to pass the credentials from the client to the web service. Now when I call iMJContactWebService.UploadDocumentToSharePoint my credentials are passed on.

...
iMJContactWebService = New wsMJ_ContactServices.Service
iMJContactWebService.Credentials = System.Net.CredentialCache.DefaultCredentials ' see Setting credentials http://support.microsoft.com/default.aspx?scid=kb;en-us;813834
...

Heres the modified ws code:


Public Function UploadDocumentToSharePoint(ByRef asDocName As String, ByVal asdocumentContents As String) As String
' 20060601 R.Chauvin bob_chauvin@yahoo.com
' adapted from http://blogs.msdn.com/erikaehrli/archive/2006/05/04/SharePointUploadHelper.aspx?CommentPosted=true#commentmessage
Dim svcDocLoader As DocumentLoader.SPFiles = New DocumentLoader.SPFiles()
svcDocLoader.PreAuthenticate = True
svcDocLoader.Credentials = CredentialCache.DefaultCredentials

'Dim strPath As String = asdocumentContents
'Dim strFile As String = strPath.Substring(strPath.LastIndexOf("\\") + 1)
Dim strDestination As String = _sharepointDocumentLibrary
If as_doclibpath = "" Then as_doclibpath = _sharepointDocumentLibrary

Dim binFile() As [Byte] = System.Text.Encoding.UTF8.GetBytes(asdocumentContents)

Dim result As String = svcDocLoader.UploadDocument(asDocName, binFile, strDestination)
asDocName = strDestination & "/" & asDocName

Return (result)

End Function

Finally, I needed to modify the WSCheckout sample webservice from Microsoft to allow for document updates as well as adds. After some research, I found the WSS v2 SDK had all the methods laid out for the SPFile class, including the Add method with it's optional overwrite parameter.

After a simple change (with some enhanced return value code), the method now Adds and Updates to the document library. If you have enabled versioning in the sharepoint document library, updates will create a new version of the file.


public string UploadDocument(string fileName, byte[] fileContents, string targetFolder)
{

string ls_Return, ls_fileURL;
bool lb_Found = false;

if (fileContents == null)
{
return "Null Attachment";
}
try
{
SPFolder folder = targetWebSite.GetFolder(targetFolder);
ls_fileURL = targetFolder + '\\' + fileName;

try
{
SPFile file = GetFile(ls_fileURL);
lb_Found = true;
}
catch (Exception ex)
{
// file not found exception

} //try

if (lb_Found)
{
SPFile newFile = folder.Files.Add(fileName, fileContents, true);
ls_Return = newFile.Title + "; Updated " + newFile.TimeCreated.ToLongDateString(); //+ "; guid=" + newFile.UniqueID();;
}
else
{
SPFile newFile = folder.Files.Add(fileName, fileContents);
ls_Return = newFile.Title + "; Created " + newFile.TimeLastModified.ToLongDateString(); //+ "; guid=" + newFile.UniqueID();;

}


return ls_Return;
}
catch (System.Exception ee)
{
return "Exception: " + ee.Message + "; " + ee.Source;
}
}


Thanks again to Erika Ehril for getting me started...

3 Comments:

Blogger Bob C said...

I have this working under V3 as well. No big supriese, as the API is fairly backward compatable.

The biggist issue was getting the service to run in the v3 site context. I solved this by creating a virtual path under the v3 site which points to the desired service.

More details later.

6:35 PM  
Blogger Mike said...

I have a very similar service running. I can't seem to ever get files to overwrite though. Any suggestions?

4:26 PM  
Blogger Bob C said...

Mike,

Give me a day or so. My guess is the wss api call
SPFile newFile = folder.Files.Add(fileName, fileContents, true);

needs a good look. Check msdn, google, etc.

5:34 PM  

Post a Comment

<< Home