public override void DoAfterPublishUploadWork(IFileUploadContext uploadContext) { FileAttachSettings attachSettings = new FileAttachSettings(uploadContext.Settings); if (attachSettings.AttachmentFileName == null) { CalculateUploadVariables(uploadContext.FormatFileName(uploadContext.PreferredFileName), attachSettings); } string listGuid = SharepointBlogIdToListGuid(uploadContext.BlogId); if (listGuid != null) { SharePointListsService listsServicesharePointLists = new SharePointListsService(attachSettings.UploadServiceUrl); listsServicesharePointLists.Credentials = GetHttpCredentials(); //The AddAttachment() call will throw an error if the attachment already exists, so we need to delete //the attachment first (if it exists). To delete the attachment, we must construct the attachment URL //that is typically generated internally by the server. //Sample URL: http://sharepoint/sites/writer/b2/blog/Lists/Posts/Attachments/13/Sunset_thumb1.jpg string attachDeleteUrl = String.Format(CultureInfo.InvariantCulture, "{0}{1}/Lists/Posts/Attachments/{2}/{3}", attachSettings.BaseUrl, attachSettings.BlogUrlPart, uploadContext.PostId, attachSettings.AttachmentFileName); try { listsServicesharePointLists.DeleteAttachment(listGuid, uploadContext.PostId, attachDeleteUrl); } catch (Exception) {} //Add the attachment using (Stream fileContents = uploadContext.GetContents()) listsServicesharePointLists.AddAttachment(listGuid, uploadContext.PostId, attachSettings.AttachmentFileName, Convert.ToBase64String(StreamHelper.AsBytes(fileContents))); uploadContext.Settings.SetString(uploadContext.PostId, FILE_ALREADY_UPLOADED); return; } throw new BlogClientFileUploadNotSupportedException(); }
public override void DoAfterPublishUploadWork(IFileUploadContext uploadContext) { FileAttachSettings attachSettings = new FileAttachSettings(uploadContext.Settings); if (attachSettings.AttachmentFileName == null) { CalculateUploadVariables(uploadContext.FormatFileName(uploadContext.PreferredFileName), attachSettings); } string listGuid = SharepointBlogIdToListGuid(uploadContext.BlogId); if (listGuid != null) { SharePointListsService listsServicesharePointLists = new SharePointListsService(attachSettings.UploadServiceUrl); listsServicesharePointLists.Credentials = GetHttpCredentials(); //The AddAttachment() call will throw an error if the attachment already exists, so we need to delete //the attachment first (if it exists). To delete the attachment, we must construct the attachment URL //that is typically generated internally by the server. //Sample URL: http://sharepoint/sites/writer/b2/blog/Lists/Posts/Attachments/13/Sunset_thumb1.jpg string attachDeleteUrl = String.Format(CultureInfo.InvariantCulture, "{0}{1}/Lists/Posts/Attachments/{2}/{3}", attachSettings.BaseUrl, attachSettings.BlogUrlPart, uploadContext.PostId, attachSettings.AttachmentFileName); try { listsServicesharePointLists.DeleteAttachment(listGuid, uploadContext.PostId, attachDeleteUrl); } catch (Exception) { } //Add the attachment using (Stream fileContents = uploadContext.GetContents()) listsServicesharePointLists.AddAttachment(listGuid, uploadContext.PostId, attachSettings.AttachmentFileName, Convert.ToBase64String(StreamHelper.AsBytes(fileContents))); uploadContext.Settings.SetString(uploadContext.PostId, FILE_ALREADY_UPLOADED); return; } throw new BlogClientFileUploadNotSupportedException(); }