Пример #1
0
        private static void FillLocalList(ClientContext context, List list, LocalList localList)
        {
            //Create a itempos
            ListItemCollectionPosition itemPosition = null;

            while (true)
            {
                // This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll"
                // so that it grabs all list items, regardless of the folder they are in.
                CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
                query.ListItemCollectionPosition = itemPosition;

                ListItemCollection items = list.GetItems(query);

                // Retrieve all items in the ListItemCollection from List.GetItems(Query).
                context.Load(items, itema => itema.Include(i => i.FieldValuesAsText), itemb => itemb.ListItemCollectionPosition);
                context.ExecuteQuery();

                itemPosition = items.ListItemCollectionPosition;

                foreach (ListItem listItem in items)
                {
                    localList.Items.AddLast(new LocalListItem(listItem.FieldValuesAsText.FieldValues));
                }
                if (itemPosition == null)
                {
                    break;
                }
            }
        }
Пример #2
0
        public static bool SaveLocalListToCSV(LocalList list, bool includeHeader, string fileName)
        {
            if (list == null || list.Items.Count == 0 || System.IO.File.Exists(fileName))
            {
                return(false);
            }


            using (StreamWriter writer = new StreamWriter(fileName, false))
            {
                if (writer == null)
                {
                    return(false);
                }

                if (includeHeader)
                {
                    string[] columnNames = list.Items.First().Fields.Keys.Select(column => column == null ? string.Empty : "\"" + column.Replace("\n", string.Empty).Replace("\"", "\"\"") + "\"").ToArray <string>();
                    writer.WriteLine(String.Join(",", columnNames));
                    writer.Flush();
                }

                foreach (LocalListItem row in list.Items)
                {
                    string[] fields = row.Fields.Values.Select(field => field == null? string.Empty : "\"" + field.ToString().Replace("\n", string.Empty).Replace("\"", "\"\"") + "\"").ToArray <string>();
                    writer.WriteLine(String.Join(",", fields));
                    writer.Flush();
                }
            }
            return(true);
        }
Пример #3
0
        private static void SaveFilesFromList(ClientContext context, LocalList list, string localRootPath)
        {
            //!string.IsNullOrEmpty(listItem.Fields["FileLeafRef"]) &&

            if (list.Items.Count == 0)
            {
                return;
            }
            if (list.Items.First().Fields.ContainsKey("FileLeafRef") && list.Items.First().Fields.ContainsKey("FileRef"))
            {
                foreach (var listItem in list.Items)
                {
                    if (listItem.Fields["ContentTypeId"].Contains("0x0100"))
                    {
                        //Document ContentTypeID
                        log.InfoFormat("Downloading {0} ", listItem.Fields["FileLeafRef"]);
                        string localFilePath = LocalFileLocation(listItem.Fields["FileRef"], true);

                        if (!System.IO.File.Exists(localFilePath))
                        {
                            Microsoft.SharePoint.Client.FileInformation f = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, listItem.Fields["FileRef"]);

                            using (var fileStream = new FileStream(localFilePath, FileMode.Create))
                            {
                                f.Stream.CopyTo(fileStream);
                            }
                            log.Info(" Completed!");
                        }
                        else
                        {
                            log.Info(" already existed!");
                        }
                    }
                }
            }
        }
Пример #4
0
        static int SomeMain(string[] args)
        {
            SPAttachmentDownloader downloader = new SPAttachmentDownloader();

            downloader.DownloadAttachments();

            SiteUrl  = "https://tridentcrm1.sharepoint.com";
            UserName = "******";

            args = new string[] { "https://tridentcrm1.sharepoint.com", "*****@*****.**",
                                  "Arch*05012013", @"C:\Users\vinn\Documents\sharepointdocs", "Custom List" };

            try
            {
                if (!ReadArguments(args))
                {
                    return(1);
                }
                // Starting with context, the constructor requires a URL to the
                // server running SharePoint.
                using (ClientContext context = new ClientContext(SiteUrl))
                {
                    context.Credentials = new SharePointOnlineCredentials(UserName, Password);

                    //Load Libraries from SharePoint
                    context.Load(context.Web.Lists);
                    context.ExecuteQuery();
                    log.InfoFormat("Found {0} lists", context.Web.Lists.Count);

                    foreach (List list in context.Web.Lists)
                    {
                        log.InfoFormat("Processing list {0}", list.Title);

                        if (list.Title == ListName)
                        {
                            //bool docLib = (list.BaseType == BaseType.DocumentLibrary);

                            LocalList localList = new LocalList();
                            localList.Title = list.Title;

                            FillLocalList(context, list, localList);
                            log.InfoFormat("Downloaded listdata. Found {0} list items", localList.Items.Count);

                            string localFilePath = LocalFileLocation(list.ParentWebUrl, false);

                            localFilePath = Path.Combine(localFilePath, localList.Title + ".csv");

                            SaveLocalListToCSV(localList, true, localFilePath);
                            log.InfoFormat("Saved CSV {0}", localFilePath);


                            log.Info("Downloading doclib items");
                            SaveFilesFromList(context, localList, LocalRootFolder);
                            log.Info("Downloading doclib items done!");
                        }
                        //log.InfoFormat("Processing list {0}", list.Title);
                    }
                }
                log.InfoFormat("Finished successful for site {0}", SiteUrl);
                return(0);
            }
            catch (Exception ex)
            {
                log.Error("Terminated with error for site " + SiteUrl, ex);
                return(2);
            }
        }