static void Main(string[] args)
        {
            Console.Title = "Download Blog Document";
            Console.WriteLine(string.Format("Start {0}", DateTime.Now));
            DBM       db = new DBM();
            DataTable dt = null;

            if (args.Length == 0)
            {
                dt = db.getListItemDocuments("", "");
            }
            else
            {
                dt = db.getListItemDocumentsForSpecificWeb(args[0].ToString());
            }



            foreach (DataRow dr in dt.Rows)
            {
                string tempfilepath = dr["FilePath"].ToString();
                string hostName     = GetHostUrl(dr["SITECOLLECTION"].ToString(), dr["SUBSITE"].ToString());
                // Uri filename = new Uri(dr["SiteUrl"].ToString() +"/"+ tempfilepath);
                Uri    filename       = new Uri(hostName + tempfilepath);
                string server         = filename.AbsoluteUri.Replace(filename.AbsolutePath, "");
                string serverrelative = filename.AbsolutePath;

                Console.WriteLine(filename.AbsolutePath);
                string downloadlocationfilepath = ConfigurationManager.AppSettings["DownloadLocation"].ToString() + dr["RowID"].ToString() + "_" + serverrelative.Substring(serverrelative.LastIndexOf("/") + 1);
                try
                {
                    Microsoft.SharePoint.Client.ClientContext clientContext =
                        new Microsoft.SharePoint.Client.ClientContext(server);
                    Microsoft.SharePoint.Client.FileInformation f =
                        Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, serverrelative);

                    clientContext.ExecuteQuery();

                    using (var fileStream = new FileStream(downloadlocationfilepath, FileMode.Create))
                        f.Stream.CopyTo(fileStream);
                    db.updateListItemDocumentDownloadStatus(dr["RowID"].ToString(), downloadlocationfilepath, dr["ListDefinationRowID"].ToString());
                }
                catch (Exception)
                {
                    db.updateListItemDocumentDownloadStatus(dr["RowID"].ToString(), null, dr["ListDefinationRowID"].ToString());
                }
            }
            Console.WriteLine(string.Format("End {0}", DateTime.Now));
            // Console.Read();
        }
Example #2
0
 public override Stream GetStream(string fileName)
 {
     try
     {
         if (TryGetFileByServerRelativeUrl(_web, _web.ServerRelativeUrl + "/" + LibraryName + "/" + fileName, out SP.File spFile))
         {
             SharepointClientContext.Load(spFile);
             SharepointClientContext.ExecuteQuery();
             using (SP.FileInformation fileInfo = SP.File.OpenBinaryDirect(_clientContext, spFile.ServerRelativeUrl))
             {
                 return(fileInfo.Stream);
             }
         }
     }
     catch
     {
         return(null);
     }
     return(null);
 }
        public void Process(JObject command, Context context)
        {
            var    siteurl                 = JSONUtil.GetText(command, "url");
            var    clientContext           = new SP.ClientContext(siteurl);
            string serverRelativeUrlOfFile = JSONUtil.GetText(command, "sourcefile");
            string fileDestinationPath     = JSONUtil.GetText(command, "destinationfile");

            clientContext.Credentials = CredentialCache.DefaultNetworkCredentials;
            var un = JSONUtil.GetText(command, "un");
            var p  = new SecureString();
            var pw = JSONUtil.GetText(command, "pw");

            foreach (var c in pw)
            {
                p.AppendChar(c);
            }

            // doesn't appear to work for non onmicrosoft.com (corp) credentials
            // need cookies?
            clientContext.Credentials = new SharePointOnlineCredentials(un, p);

            using (SP.FileInformation sharePointFile =
                       SP.File.OpenBinaryDirect(clientContext, serverRelativeUrlOfFile))
            {
                using (Stream destFile = System.IO.File.OpenWrite(fileDestinationPath))
                {
                    byte[] buffer = new byte[8 * 1024];
                    int    byteReadInLastRead;
                    while ((byteReadInLastRead = sharePointFile.Stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        destFile.Write(buffer, 0, byteReadInLastRead);
                    }
                }
            }

            System.Diagnostics.Process.Start(fileDestinationPath);
        }
Example #4
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!");
                        }
                    }
                }
            }
        }
        public DownloadFileResponse DownloadFile(DownloadFileRequest request)
        {
            using (ClientContext context = SignIn(request.ServerProperties))
            {
                try
                {
                    Microsoft.SharePoint.Client.List root = context.Web.Lists.GetByTitle(SharedDocumentsList);
                    string downloadUri = GetCombinedPath(context, root, request.FileUri.ToString());

                    byte[] data = null;
                    using (Microsoft.SharePoint.Client.FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, downloadUri))
                    {
                        using (var ms = new MemoryStream())
                        {
                            fileInfo.Stream.CopyTo(ms);
                            data = ms.ToArray();
                        }
                    }

                    string base64 = null;
                    if (data != null)
                    {
                        base64 = System.Convert.ToBase64String(data);
                    }

                    return(new DownloadFileResponse
                    {
                        Data = base64
                    });
                }
                catch
                {
                    throw;
                }
            }
        }
        public static List <Models.Document> DocsInList()
        {
            string webSPOUrl = "https://yourdevsite";
            string userName  = "******";
            string password  = "******";

            //TODO: set up Azure AD login

            int count = 0;

            string text = "";

            List <Models.Document> docsInList = new List <Models.Document>();

            using (var clientContext = new SP.ClientContext(webSPOUrl))
            {
                clientContext.Credentials = new SP.SharePointOnlineCredentials(userName, ToSecureString(password));

                SP.Web web = clientContext.Web;

                SP.List sharedDocumentsList = clientContext.Web.Lists.GetByTitle("Dokument");

                SP.CamlQuery camlQuery = new SP.CamlQuery();

                SP.ListItemCollection listItems = sharedDocumentsList.GetItems(camlQuery);

                clientContext.Load(sharedDocumentsList);

                clientContext.Load(listItems);

                clientContext.ExecuteQuery();

                if (listItems.Count != 0)
                {
                    foreach (SP.ListItem item in listItems)
                    {
                        count++;

                        string fileName = (String)item["FileLeafRef"];

                        SP.FileInformation fileInformation = SP.File.OpenBinaryDirect(clientContext, (string)item["FileRef"]);

                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            CopyStream(fileInformation.Stream, memoryStream);

                            using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, false))
                            {
                                var firstParagraph = doc.MainDocumentPart.Document.Body;

                                if (firstParagraph != null)
                                {
                                    text = firstParagraph.InnerText;
                                }
                                else
                                {
                                    text = "Document doesn't contain any paragraphs.";
                                }
                            }
                        }
                        docsInList.Add(new Models.Document(count, fileName, text));
                    }
                }
                else
                {
                    text = "Document not found.";
                }
            }
            return(docsInList);
        }
        public void DownloadAttachments()
        {
            try
            {
                //int startListID;
                //Console.WriteLine("Enter Starting List ID");
                //if (!Int32.TryParse(Console.ReadLine(), out startListID))
                //{
                //    Console.WriteLine("Invalid ID");
                //    Console.WriteLine("Press any key to exit...");
                //    Console.ReadKey();
                //    return;
                //}

                string pass = ConfigurationManager.AppSettings["Password"];

                String       siteUrl  = "";
                String       listName = "";
                SecureString Password = new SecureString();


                foreach (char c in pass.ToCharArray())
                {
                    Password.AppendChar(c);
                }
                Password.MakeReadOnly();

                using (ClientContext clientContext = new ClientContext(siteUrl))
                {
                    clientContext.Credentials = new SharePointOnlineCredentials("*****@*****.**", Password);
                    Console.WriteLine("Started Attachment Download " + siteUrl);
                    logger.Info("Started Attachment Download" + siteUrl);
                    //clientContext.Credentials = credentials;

                    //Get the Site Collection
                    Site oSite = clientContext.Site;
                    clientContext.Load(oSite);
                    clientContext.ExecuteQuery();

                    // Get the Web		CustomizedPageStatus	None	Microsoft.SharePoint.Client.CustomizedPageStatus

                    Web oWeb = clientContext.Web;
                    clientContext.Load(oWeb);
                    clientContext.ExecuteQuery();

                    Microsoft.SharePoint.Client.File fl = clientContext.Web.GetFileByServerRelativeUrl("");
                    clientContext.Load(fl);
                    clientContext.ExecuteQuery();

                    ClientResult <String> result = fl.ListItemAllFields.GetWOPIFrameUrl(SPWOPIFrameAction.View);
                    string url = fl.ListItemAllFields.ContentType.ToString();
                    clientContext.Load(fl.ListItemAllFields);
                    clientContext.ExecuteQuery();

                    CamlQuery query = new CamlQuery();
                    query.ViewXml = @"";

                    List oList = clientContext.Web.Lists.GetByTitle(listName);
                    clientContext.Load(oList);
                    clientContext.ExecuteQuery();

                    ListItemCollection items = oList.GetItems(query);
                    clientContext.Load(items);
                    clientContext.ExecuteQuery();


                    //Get WOPI URL
                    var query2 = new CamlQuery();
                    query.ViewXml = "...";

                    var listItems = oList.GetItems(query2);
                    clientContext.Load(listItems);
                    clientContext.ExecuteQuery();

                    var wopiUrls = new Dictionary <ListItem, ClientResult <string> >();

                    // The useWopi flag is set when WOPI URLs are desired

                    foreach (var listItem in listItems)
                    {
                        wopiUrls[listItem] = listItem.GetWOPIFrameUrl(SPWOPIFrameAction.Edit);
                        clientContext.Load(listItem, item => item.Id);
                    }
                    // This query includes Id, FileSystemObjectType, DisplayName, and Modified
                    //clientContext.ExecuteQuery();


                    //string relativeUrl = oWeb.ServerRelativeUrl;
                    //Folder retrievedFolder = oWeb.GetFolderByServerRelativeUrl(relativeUrl);
                    //clientContext.Load(retrievedFolder);
                    //clientContext.ExecuteQuery();

                    //CamlQuery camlQuery = new CamlQuery();
                    //camlQuery.ViewXml = @"";

                    //Use this statement if List is completely fresh and has no list items i.e no folders are present
                    //under the given list
                    if (oList.ItemCount == 0)
                    {
                        DateTime dt            = DateTime.Now;
                        string   date          = dt.ToShortDateString().ToString();
                        string   modifiedDate  = date.Replace("/", "-");
                        string   newFolderName = "HBApplication " + modifiedDate;

                        Folder newFolder = oList.RootFolder.Folders.Add(newFolderName);
                        clientContext.Load(newFolder);
                        clientContext.ExecuteQuery();
                    }

                    //Use this statement if the given list will always have some items i.e folder present in the list
                    foreach (ListItem listItem in items)
                    {
                        //if (Int32.Parse(listItem["ID"].ToString()) >= startListID)
                        //{

                        DateTime dt             = DateTime.Now;
                        string   date           = dt.ToShortDateString().ToString();
                        string   existingFolder = listItem["FileLeafRef"].ToString();
                        string   modifiedDate   = date.Replace("/", "-");
                        string   folderToCheck  = "HBApplication " + modifiedDate;

                        if (!existingFolder.Contains(modifiedDate))
                        {
                            string newFolderName = folderToCheck;

                            List docs = clientContext.Web.Lists.GetByTitle(listName);
                            clientContext.Load(docs, l => l.RootFolder);
                            clientContext.Load(docs.RootFolder, l => l.Folders);

                            docs.EnableFolderCreation = true;
                            docs.Update();
                            clientContext.ExecuteQuery();


                            Folder newFolder = docs.RootFolder.Folders.Add(newFolderName);
                            clientContext.Load(newFolder);
                            clientContext.ExecuteQuery();
                        }

                        else
                        {
                            Console.WriteLine("Folder already there !");
                        }



                        //string name = listItem["title"].ToString();

                        Console.WriteLine("Process Attachments for ID " +
                                          listItem["ID"].ToString());

                        Folder folder =
                            oWeb.GetFolderByServerRelativeUrl("/CRM Documents/");

                        try
                        {
                            clientContext.ExecuteQuery();
                        }
                        catch (ServerException ex)
                        {
                            logger.Info(ex.Message);
                            Console.WriteLine(ex.Message);
                            logger.Info("No Attachment for ID " + listItem["ID"].ToString());
                            Console.WriteLine("No Attachment for ID " + listItem["ID"].ToString());
                        }

                        FileCollection attachments = folder.Files;
                        clientContext.Load(attachments);
                        clientContext.ExecuteQuery();

                        foreach (Microsoft.SharePoint.Client.File oFile in folder.Files)
                        {
                            logger.Info("Found Attachment for ID " +
                                        listItem["ID"].ToString());

                            Console.WriteLine("Found Attachment for ID " +
                                              listItem["ID"].ToString());

                            FileInfo  myFileinfo = new FileInfo(oFile.Name);
                            WebClient client1    = new WebClient();
                            //client1.Credentials = credentials;

                            logger.Info("Downloading " +
                                        oFile.ServerRelativeUrl);

                            Console.WriteLine("Downloading " +
                                              oFile.ServerRelativeUrl);

                            string localFilePath = LocalFileLocation(oFile.ServerRelativeUrl, true);


                            if (!System.IO.File.Exists(localFilePath))
                            {
                                Microsoft.SharePoint.Client.FileInformation f = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, oFile.ServerRelativeUrl);

                                using (var fileStream = new FileStream(localFilePath, FileMode.Create))
                                {
                                    f.Stream.CopyTo(fileStream);
                                }
                                Console.WriteLine("Completed!");
                            }
                            else
                            {
                                Console.WriteLine(" already existed!");
                            }
                        }
                        //}
                    }
                }
            }
            catch (Exception e)
            {
                logger.ErrorException(e.Message, e);
                logger.Error(e.StackTrace);
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
            }
        }