コード例 #1
0
ファイル: SharePointManager.cs プロジェクト: trungpv/SPSync
        public static string[] GetAllFoldersFromConfig(SyncConfiguration configuration)
        {
            var folderList = new List <string>();

            var spm = configuration.GetSharePointManager();

            using (var context = spm.GetClientContext())
            {
                var web = context.Web;
                context.Load(web);
                var list = context.Web.Lists.GetByTitle(configuration.DocumentLibrary);
                context.Load(list, p => p.RootFolder.ServerRelativeUrl, p => p.RootFolder.Name, p => p.RootFolder.Folders);
                context.ExecuteQuery();

                var subFolderList = GetAllFoldersInternal(context, list.RootFolder);
                subFolderList.ForEach(f =>
                {
                    var folder = f;
                    if (web.ServerRelativeUrl != "/")
                    {
                        folder = f.Substring(web.ServerRelativeUrl.Length);
                    }
                    folder = folder.Substring(folder.IndexOf("/", 1));
                    folderList.Add(folder);
                });
            }

            return(folderList.ToArray());
        }
コード例 #2
0
 public SyncManager(string localFolder)
 {
     _originalFolder    = localFolder;
     _configuration     = SyncConfiguration.FindConfiguration(localFolder);
     _localFolder       = _configuration.LocalFolder;
     _sharePointManager = _configuration.GetSharePointManager();
     _metadataStore     = new MetadataStore(_localFolder);
 }
コード例 #3
0
ファイル: SharePointManager.cs プロジェクト: trungpv/SPSync
        public static SyncConfiguration TryFindConfiguration(string url, string username, string password)
        {
            var syncConfig = new SyncConfiguration();
            var docLibUrl  = "n/a";

            try
            {
                syncConfig.Username = username;
                syncConfig.Password = password;
                syncConfig.SiteUrl  = url;

                if (username.Contains("\\"))
                {
                    var split = username.Split('\\');
                    syncConfig.Username           = split[1];
                    syncConfig.Domain             = split[0];
                    syncConfig.AuthenticationType = AuthenticationType.NTLM;
                }
                else
                {
                    if (url.ToLower().Contains(".sharepoint.com"))
                    {
                        syncConfig.AuthenticationType = AuthenticationType.Office365;
                    }
                }

                //https://mytenant.sharepoint.com/personal/user_mytenant_onmicrosoft_com/_layouts/15/start.aspx#/Documents/Forms/All.aspx
                var siteUrl        = url.ToLower();
                var documentLibUrl = string.Empty;
                if (siteUrl.Contains("/_layouts/15/start.aspx"))
                {
                    documentLibUrl = siteUrl.Substring(siteUrl.IndexOf("#/") + 2);
                    documentLibUrl = documentLibUrl.Substring(0, documentLibUrl.IndexOf("/"));
                    siteUrl        = siteUrl.Substring(0, siteUrl.IndexOf("/_layouts/15/start.aspx"));
                }
                else
                {
                    if (siteUrl.Contains("/forms/"))
                    {
                        siteUrl        = siteUrl.Substring(0, siteUrl.IndexOf("/forms/"));
                        documentLibUrl = siteUrl.Substring(siteUrl.LastIndexOf("/") + 1);
                        siteUrl        = siteUrl.Substring(0, siteUrl.LastIndexOf("/"));
                    }
                    else
                    {
                        var s = siteUrl;
                        siteUrl        = siteUrl.Substring(0, siteUrl.LastIndexOf("/"));
                        documentLibUrl = s.Substring(s.LastIndexOf("/") + 1);
                    }
                }

                docLibUrl = documentLibUrl;

                syncConfig.SiteUrl             = siteUrl;
                syncConfig.ConflictHandling    = ConflictHandling.ManualConflictHandling;
                syncConfig.DownloadHeadersOnly = false;
                syncConfig.SelectedFolders     = null;

                var spm = syncConfig.GetSharePointManager();
                using (var context = spm.GetClientContext())
                {
                    Logger.LogDebug("TryFindConfiguration ClientContext acquired");
                    var web = context.Web;
                    context.Load(web);
                    context.ExecuteQuery();

                    var listUrl = web.ServerRelativeUrl + "/" + documentLibUrl;
                    if (web.ServerRelativeUrl == "/")
                    {
                        listUrl = "/" + documentLibUrl;
                    }

                    Logger.LogDebug("TryFindConfiguration ClientContext web loaded. GetList={0}", listUrl);

                    var list = web.GetList(listUrl);
                    try
                    {
                        context.Load(list);
                        context.ExecuteQuery();

                        syncConfig.DocumentLibrary = list.Title;
                        Logger.LogDebug("TryFindConfiguration Found lib title={0}", list.Title);
                    }
                    catch
                    {
                        Logger.LogDebug("TryFindConfiguration Url does not contain a list. Trying to find sub web...");

                        var allSubWebs = web.Webs;
                        context.Load(allSubWebs, p => p.Include(x => x.ServerRelativeUrl));
                        context.ExecuteQuery();

                        var subWeb = allSubWebs.ToList().FirstOrDefault(p => p.ServerRelativeUrl.Contains(listUrl));

                        if (subWeb == null)
                        {
                            return(null);
                        }

                        Logger.LogDebug("TryFindConfiguration Sub web found. Loading all lists...");

                        var allLists = subWeb.Lists;
                        context.Load(allLists);
                        context.ExecuteQuery();

                        var allDocLibs = allLists.ToList().Where(p => p.BaseType == BaseType.DocumentLibrary && !p.Hidden);

                        syncConfig.DocumentLibrary = string.Join("|", allDocLibs.Select(p => p.Title));
                        Logger.LogDebug("TryFindConfiguration All doc libs loaded: {0}", syncConfig.DocumentLibrary);
                    }
                }

                return(syncConfig);
            }
            catch (ArgumentException)
            {
                throw;
            }
            catch (IdcrlException)
            {
                throw;
            }
            catch (WebException)
            {
                throw;
            }
            catch (Exception ex)
            {
                Logger.LogDebug("TryFindConfiguration Error: Url={0} AuthType={1} SiteUrl={2} DocLibUrl={3} Username={4}", url, syncConfig.AuthenticationType, syncConfig.SiteUrl, docLibUrl, syncConfig.Username);
                Logger.Log("[{3}] Failed to get config automatically: {0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace, DateTime.Now);

                return(null);
            }
        }