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...

Friday, June 16, 2006

VSTO v2 and v3, Cypress, Version Compatability?

I'm just starting to review these apps. Some starting points:

) My current environment includes:
  • Office 2003
  • VSTO 2005 for Office 2003
  • Outlook 2003 Add-in
  • Infopath 2003 forms with managed code.
  • Infopath 2007 Beta

) The "preferred"MSDN Newsgroup for VSTO Questions.

) When I try to install the Cypress release, the installer complains with:
"The Microsoft Office InfoPath 2003 Toolkit for Visual Studio 2005 must be uninstalled."

) I have posted to the newsgroup trying to verify that I wont loose compatability with my Office 2003 development efforts. The initial feedback indicates that cypress is NOT backward compatable..

Wednesday, June 14, 2006

This is MUST HAVE in your toolbox. C# to VB.Net

Check out this site http://www.KamalPatel.net/ .

The C# to VB.NET saved me many hours. Many thanks Kamal Patel!