When we create a Office 2010 addin say for word and try to access SharePoint resources from it we know pretty much better option is 'Client Object model'.
1. Create a new Word 2010 addin and a button called 'Save'
2. Include a win form and add a button called "Upload doc"
3. Call this form on click of Save button.
4 Below code is to upload a current open word document in Office 2010 to a SharePoint site without saving locally on click of Upload Doc button.
public void UploadDocument(byte[] docbytes)
{
ClientContext clientContext = new ClientContext("http://sharepointsiteurl/");
List documentsList = clientContext.Web.Lists.GetByTitle("Events");
var fileCreationInformation = new FileCreationInformation();
fileCreationInformation.Content = docbytes;
fileCreationInformation.Overwrite = true;
fileCreationInformation.Url = "http://sharepointdoclibrarylocation/la.doc";
Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);
uploadFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();
1. Create a new Word 2010 addin and a button called 'Save'
2. Include a win form and add a button called "Upload doc"
3. Call this form on click of Save button.
4 Below code is to upload a current open word document in Office 2010 to a SharePoint site without saving locally on click of Upload Doc button.
public void UploadDocument(byte[] docbytes)
{
ClientContext clientContext = new ClientContext("http://sharepointsiteurl/");
List documentsList = clientContext.Web.Lists.GetByTitle("Events");
var fileCreationInformation = new FileCreationInformation();
fileCreationInformation.Content = docbytes;
fileCreationInformation.Overwrite = true;
fileCreationInformation.Url = "http://sharepointdoclibrarylocation/la.doc";
Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);
uploadFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();
}
and on button click below code grabs the current document content as byte array and calls the above function to upload it to SharePoint
private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data.GetData(DataFormats.Html).ToString());
MessageBox.Show("Before Upload");
try
{
UploadDocument(bytes);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
MessageBox.Show("Uploaded to SharePoint Successfully");
}
The difference from other solutions is here we dont save the document anywhere locally instead directly grabbing the content as byte array and uploading it to SharePoint doc library which will improve the performance drastically.
No comments:
Post a Comment