En av de saker jag inte hann med att visa på "Innovation Day: SharePoint 2007" var hur man kan skicka upp en fil via en webbtjänst till SharePoint 2007.
En finess med att SharePoint 2007 bygger på ASP.NET 2.0 är att vi enkelt kan lägga till funktionalitet som baseras på samma teknik.
Ett exempel är att skapa en egen ASP.NET 2.0 Web Service som använder objektmodellen i SharePoint 2007 för att lägga till en fil i en lista.
För att åstadkomma detta gör följande:
Web Service
[WebMethod]public string UploadDocument(string fileName, byte[] fileContents, string pathFolder){if (fileContents == null){return "Null Attachment"}try{int iStartIndex = pathFolder.LastIndexOf("/");string sitePath = pathFolder.Remove(iStartIndex);string folderName = pathFolder.Substring(iStartIndex + 1);
SPSite site = new SPSite(sitePath);SPWeb web = site.OpenWeb();
SPFolder folder = web.GetFolder(folderName);
string fileURL = fileName;
folder.Files.Add(fileURL, fileContents);
folder.Update();
return "File added successfully!"
}catch (System.Exception ex){return ex.Source + ":" + ex.Message;}}
WindowsApplikation
try{WindowsApplication1.upload.upload oUploader = new WindowsApplication1.upload.upload();
oUploader.PreAuthenticate = true;oUploader.Credentials = CredentialCache.DefaultCredentials;
string strPath = @"C:\homepage.gif"string strFile = strPath.Substring(strPath.LastIndexOf("\\") + 1);
string strDestination = http://localhost/Shared%20Documents
FileStream fStream = new FileStream(strPath, System.IO.FileMode.Open);byte[] binFile = new byte[(int)fStream.Length];fStream.Read(binFile, 0, (int)fStream.Length);fStream.Close();
string str = oUploader.UploadDocument(strFile, binFile, strDestination);MessageBox.Show(str);
}catch (Exception ex){MessageBox.Show(ex.Source + " - " + ex.Message + " - " + ex.InnerException + " - " + ex.StackTrace);}