コード例 #1
0
        /// <summary>
        /// Sets the metadata on a given document
        /// </summary>
        /// <param name="documentId">The document ID to set metadata on</param>
        /// <param name="documentMetadata">A DocumentMetadata object to apply to the document</param>
        /// <returns></returns>
        public DocumentResult UpdateDocumentMetadata(string documentId, DocumentMetadata documentMetadata)
        {
            DocumentResult result = new DocumentResult {
                Success = false, DocumentId = documentId
            };

            // Get the item by using its ID
            SPListItem item = GetDocumentItemFromId(documentId);

            if (item != null)
            {
                // Get the author from the metadata object, update the author and modified date on the SharePoint side
                SPUser author = Util.GetUserFromLogin(documentMetadata.Author);
                if (author != null)
                {
                    DateTime currentTime = DateTime.Now.ToLocalTime();
                    item[Resource.FieldModifiedBy]   = author;
                    item[Resource.FieldModifiedDate] = currentTime;
                }
                // Update the item
                item.UpdateOverwriteVersion();
                result.Success = true;
            }
            else
            {
                result.ErrorMessage = string.Format("The document with id {0} could not be located for update.", documentId);
                Util.LogError(result.ErrorMessage, Util.ErrorLevel.Warning);
            }

            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Deletes a document from SharePoint
        /// </summary>
        /// <param name="documentId">The ID of the document to delete</param>
        /// <returns></returns>
        public DocumentResult DeleteDocument(string documentId)
        {
            DocumentResult result = new DocumentResult {
                Success = false, DocumentId = documentId
            };

            // Get the item by using its ID
            SPListItem documentItem = GetDocumentItemFromId(documentId);

            if (documentItem != null)
            {
                string docGuid = documentItem.UniqueId.ToString();
                using (DisabledEventsScope scope = new DisabledEventsScope())
                {
                    try
                    {
                        documentItem.Recycle();
                        result.Success = true;
                    }
                    catch (Exception e)
                    {
                        Util.LogError("DeleteDocument failed with exception:  " + e.Message);
                        result.Success = false;
                    }
                }
            }
            else
            {
                result.ErrorMessage = string.Format("The document with id {0} could not be located for delete.", documentId);
                Util.LogError(result.ErrorMessage, Util.ErrorLevel.Warning);
            }

            return(result);
        }
コード例 #3
0
        /// <summary>
        /// Creates a document in SharePoint
        /// </summary>
        /// <param name="documentMetadata">The metadata the document is to be created with</param>
        /// <param name="documentData">Byte array containing the document contents</param>
        /// <returns></returns>
        public DocumentResult CreateDocument(DocumentMetadata documentMetadata, byte[] documentData)
        {
            SPFile         file   = null;
            SPListItem     item   = null;
            DocumentResult result = null;
            Guid           siteId = SPContext.Current.Site.ID;
            Guid           webId  = SPContext.Current.Web.ID;

            // To have access to taxonomy and DocumentManagement namespaces, the site needs to be accessed with elevated privileges
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                result = new DocumentResult {
                    Success = false
                };
                using (SPSite site = new SPSite(siteId))
                {
                    using (SPWeb web = site.OpenWeb(webId))
                    {
                        web.AllowUnsafeUpdates = true;

                        /* Use the appropriate document library based on the type of the document */
                        SPList documentList = null;
                        if (documentMetadata.DocumentType == DocumentType.VendorDocument)
                        {
                            documentList = web.Lists[Resource.VendorDocumentsLibrary];
                        }
                        if (documentMetadata.DocumentType == DocumentType.WorkMatterDocument)
                        {
                            documentList = web.Lists[Resource.WorkMatterDocumentsLibrary];
                        }

                        if (documentList != null)
                        {
                            string tempId = Guid.NewGuid().ToString();
                            try
                            {
                                // If there's a document ID then the user is doing an update.
                                // When the user changes the document name in ClaimCenter, SharePoint sees it as a new document
                                if (documentMetadata.DocumentID != null)
                                {
                                    string documentUrl = GetDocumentUrlFromId(site, documentMetadata.DocumentID);
                                    file = web.GetFile(documentUrl);
                                    file.SaveBinary(documentData, false);
                                }
                                else
                                {
                                    // To allow for similarly named files in different work matters, we can append the Claim Number or Contact ID
                                    string fileName = "";
                                    if (documentMetadata.DocumentType == DocumentType.VendorDocument)
                                    {
                                        string contactID = Util.RemoveMetaCharacters(documentMetadata.ContactID);
                                        fileName         = documentMetadata.FileName.Substring(0, documentMetadata.FileName.LastIndexOf(".")) + " - " + contactID + documentMetadata.FileName.Substring(documentMetadata.FileName.LastIndexOf("."));
                                    }
                                    else if (documentMetadata.DocumentType == DocumentType.WorkMatterDocument)
                                    {
                                        fileName = documentMetadata.FileName.Substring(0, documentMetadata.FileName.LastIndexOf(".")) + " - " + documentMetadata.ClaimNumber + documentMetadata.FileName.Substring(documentMetadata.FileName.LastIndexOf("."));
                                    }

                                    // This hashtable isn't really used, it's just a required parameter for the Files.Add overload being used
                                    System.Collections.Hashtable additionalProperties = new System.Collections.Hashtable();

                                    // Try to get the author, but fall back to the service account if necessary
                                    SPUser author = Util.GetUserFromLogin(documentMetadata.Author);
                                    if (author == null)
                                    {
                                        author = SPContext.Current.Web.CurrentUser;
                                    }
                                    file = documentList.RootFolder.Files.Add(fileName, documentData, additionalProperties, author, author, DateTime.Now, DateTime.Now.ToUniversalTime(), true);
                                }
                                // Check in the file if it is checked out
                                if (file.CheckOutType != SPFile.SPCheckOutType.None)
                                {
                                    if (documentList.EnableMinorVersions)
                                    {
                                        file.CheckIn(string.Empty, SPCheckinType.MinorCheckIn);
                                    }
                                    else
                                    {
                                        file.CheckIn(string.Empty, SPCheckinType.MajorCheckIn);
                                    }
                                }
                                item = file.Item;

                                result.Success = true;
                                item.SystemUpdate(false);
                                result.DocumentId = Util.GetDocumentId(item);
                            }
                            catch (Exception e)
                            {
                                Util.LogError("CreateDocument failed with exception: " + e.Message, Util.ErrorLevel.Error);
                                result.Success      = false;
                                result.ErrorMessage = e.Message;
                            }
                            finally
                            {
                                web.AllowUnsafeUpdates = false;
                            }
                        }
                    }
                }
            });
            return(result);
        }