Пример #1
0
        /// <summary>
        /// Retrieves the server-relative URL of a folder from its absolute URL.
        /// </summary>
        /// <param name="webUrl">Web URL to establish context.</param>
        /// <param name="folderUrl">folder URL</param>
        /// <param name="username">Username for SharePoint authentication.</param>
        /// <param name="password">Password for SharePoint authentication.</param>
        /// <returns>The server-relative folder URL if found. Null otherwise.</returns>
        private string GetFolderUrlDirect(string webUrl, string folderUrl, string username, string password)
        {
            string methodName = "SharePointManager2003.GetFolderUrlDirect";

            try
            {
                SP.ClientContext context = new SP.ClientContext(webUrl);
                context.Credentials = new NetworkCredential(username, password);

                // load the web data
                SP.Web web = context.Web;
                context.Load(web);

                // load the folder data
                Uri       uri    = new Uri(folderUrl);
                SP.Folder folder = web.GetFolderByServerRelativeUrl(uri.AbsolutePath);
                context.Load(folder);

                context.ExecuteQuery();

                // check if the URL belongs to a folder
                if (folder != null)
                {
                    return(folder.ServerRelativeUrl);
                }

                return(null);
            }
            catch (Exception e)
            {
                Logger.Error(string.Format("{0}: Could not find folder with URL {1}.", methodName, folderUrl), e);
                return(null);
            }
        }
        public SP.FileCollection Attachments(SPListItem listItem, SPList list, object value)
        {
            if (value == null || !(bool)value)
            {
                return(null);
            }

            using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl)))
            {
                SP.Web web = clientContext.Web;
                clientContext.Load(web);
                SP.List splist = clientContext.ToList(list.Id);
                clientContext.Load(splist);
                SP.ListItem splistItem = splist.GetItemById(listItem.Id);
                clientContext.Load(splistItem);
                clientContext.ExecuteQuery();

                SP.Folder listFolder = splistItem.ParentList.RootFolder;
                clientContext.Load(listFolder, f => f.ServerRelativeUrl);
                clientContext.ExecuteQuery();

                SP.Folder attachmentsFolder = web.GetFolderByServerRelativeUrl(listFolder.ServerRelativeUrl + "/attachments/" + splistItem.Id.ToString());
                clientContext.Load(attachmentsFolder);
                var attachments = attachmentsFolder.Files;
                clientContext.Load(attachments);
                clientContext.ExecuteQuery();

                return(attachments);
            }
        }
Пример #3
0
 /// <summary>
 /// Sends document to user's OneDrive.
 /// </summary>
 /// <param name="web">Object of site</param>
 /// <param name="usersMySite">User's My site URL</param>
 /// <param name="key">Key parameter</param>
 /// <param name="collectionOfAttachments">Attachments Collection</param>
 /// <param name="collectionOfOriginalAttachments">Original attachments collection</param>
 /// <param name="contentTypeId">Content type id of default content type</param>
 internal static void SendDocumentToOneDrive(Microsoft.SharePoint.Client.Web web, string usersMySite, string key, Stream collectionOfAttachments, string collectionOfOriginalAttachments, string contentTypeId)
 {
     try
     {
         Microsoft.SharePoint.Client.File file = web.GetFolderByServerRelativeUrl(string.Concat(usersMySite, ServiceConstantStrings.OneDriveDocumentLibraryTitle, ConstantStrings.ForwardSlash, ServiceConstantStrings.LegalBriefcaseFolder)).Files.Add(new FileCreationInformation()
         {
             Url           = key,
             Overwrite     = true,
             ContentStream = collectionOfAttachments
         });
         file.ListItemAllFields.ParseAndSetFieldValue(ServiceConstantStrings.OneDriveSiteColumn, collectionOfOriginalAttachments);
         file.ListItemAllFields[ConstantStrings.OneDriveContentTypeProperty] = contentTypeId;
         file.ListItemAllFields.Update();
     }
     catch (Exception exception)
     {
         Logger.LogError(exception, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, ServiceConstantStrings.LogTableName);
     }
 }
Пример #4
0
        public bool DeleteFile(string fileName, string folderName)
        {
            bool   result   = false;
            string siteUrl  = SLMConstant.Ecm.SiteUrl;      //http://ecm/dept/public
            string sitePath = SLMConstant.Ecm.SitePath;     ///dept/public/OBTDocument/
            string domain   = SLMConstant.Ecm.Domain;
            string username = SLMConstant.Ecm.Username;
            string password = SLMConstant.Ecm.Password;

            try
            {
                using (ToEcm.ClientContext clientContext = new ToEcm.ClientContext(siteUrl)
                {
                    Credentials = new NetworkCredential(username, password, domain)
                })
                {
                    string serverRelativeUrlOfFile = sitePath + folderName + "/" + fileName;

                    log.Info("Delete File Path " + serverRelativeUrlOfFile);

                    ToEcm.Web web = clientContext.Web;
                    clientContext.Load(web);

                    ToEcm.File file = web.GetFileByServerRelativeUrl(serverRelativeUrlOfFile);
                    clientContext.Load(file);
                    clientContext.ExecuteQuery();

                    if (file != null)
                    {
                        file.DeleteObject();
                        clientContext.ExecuteQuery();
                    }
                    result = true;

                    //Added By Pom 24/05/2016
                    //ลบ Folder ทิ้งในกรณีที่ไม่มี file เหลือใน Folder นั้น
                    try
                    {
                        log.Info("Start Delete Folder " + sitePath + folderName + "/");
                        ToEcm.Folder folder = web.GetFolderByServerRelativeUrl(sitePath + folderName + "/");
                        clientContext.Load(folder);
                        clientContext.ExecuteQuery();

                        if (folder != null)
                        {
                            ToEcm.FileCollection fileColl = folder.Files;
                            clientContext.Load(fileColl);
                            clientContext.ExecuteQuery();

                            log.Info("Files currently in folder " + folderName + " = " + fileColl.Count.ToString());

                            if (fileColl.Count == 0)
                            {
                                folder.DeleteObject();
                                clientContext.ExecuteQuery();
                                log.Info("Folder Deleted Successfully");
                            }
                        }
                        else
                        {
                            log.Info("Folder " + folderName + " is null");
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("Error DeleteFile : Cannot delete empty folder " + folderName + ", " + ex);
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("Error DeleteFile : " + ex);
            }

            return(result);
        }