0

I need to download the files present in various SharePoint folders from SharePoint 2007 and 2010 sites present in different domains.

I am trying to write Console application using CSOM in visual Studio 2015. I am currently able to download the files from one of the domains which is in SharePoint 2007.

I am facing the below errors when I am trying to fetch the files from other domains and SharePoint 2010.

  1. when trying to connect different domain in SP 2010: The remote server returned an error: (403) Forbidden
  2. Cannot contact site at the specified URL http://sp.abc.com/projects/A101site
  3. The remote server returned an error: (500) internal server error

My Current code:

static void Main(string[] args)
{
        string ProjectURL = "http://sp.abc.com/projects/A101site";
        ClientContext site = new ClientContext(ProjectURL);                    
        site.RequestTimeout = 600000;
        Web web = site.Web;
        site.Load(web);
        site.ExecuteQuery();
        site.Load(web.Lists.GetByTitle("Documents"));
        site.ExecuteQuery();
        List list = web.Lists.GetByTitle("Documents");
        site.Load(list);
        site.ExecuteQuery();
        site.Load(list.RootFolder);
        site.ExecuteQuery();
        site.Load(list.RootFolder.Folders);
        site.ExecuteQuery();
        int i = 0;
        var relUrl = list.RootFolder.ServerRelativeUrl;
        Folder f = web.GetFolderByServerRelativeUrl(relUrl);
        FolderCollection fc = f.Folders;
        site.Load(f);
        site.ExecuteQuery();
        site.Load(fc);
        site.ExecuteQuery();
           foreach (Folder w in fc)
                    {
                        if (w.Name.Contains("SubFolder"))// && j == 0)
                        {
                            //j++;
                            site.Load(w);
                            site.ExecuteQuery();
                            site.Load(w.Folders);
                            site.ExecuteQuery();
                            foreach (Folder wsf in w.Folders)
                            {
                                site.Load(wsf);
                                site.ExecuteQuery();
                                if (wsf.Name.Contains("Actual Folder"))
                                {
         GetSubFolders(wsf.ServerRelativeUrl, i, ProjectCode, ProjectURL);
                                }
                                else continue;
                            }
                         }
                        else
                            continue;
                    }
}
public static void GetSubFolders(string folderUrl, int i, string ProjectCode, string ProjectURL)
        {
            ClientContext abssite = new ClientContext(ProjectURL);                       
            var absweb = abssite.Web;
            abssite.Load(absweb);
            abssite.ExecuteQuery();
            Folder f = absweb.GetFolderByServerRelativeUrl(folderUrl);
            FolderCollection fc = f.Folders;
            abssite.Load(f);
            abssite.ExecuteQuery();
            processFolderClientobj(f.ServerRelativeUrl, i++, ProjectCode, ProjectURL);
            abssite.Load(fc);
            abssite.ExecuteQuery();
            foreach (Folder w in fc)
            {
                abssite.Load(w.Folders);
                abssite.ExecuteQuery();
                processFolderClientobj(w.ServerRelativeUrl, i++, ProjectCode, ProjectURL);
                if (w.Folders.Count != 0)
                {
                    foreach(Folder wf in w.Folders)
                    {
                        abssite.Load(wf);
                        abssite.ExecuteQuery();
                        string rel1Url = wf.ServerRelativeUrl;
                    GetSubFolders(rel1Url, i, ProjectCode, ProjectURL);
                    }
                }
                else
                    continue;
            }
        }
public static void processFolderClientobj(string folderUrl, int i, string ProjectCode, string ProjectURL)
        {
            string Destination = @"Folder Location";
            ClientContext site = new ClientContext(ProjectURL);
            var web = site.Web;
            site.Load(web);
            site.ExecuteQuery();
            Folder folder = web.GetFolderByServerRelativeUrl(folderUrl);
            site.Load(folder);
            site.ExecuteQuery();
            site.Load(folder.Files);
            site.ExecuteQuery();

            string newName = ProjectCode;
            foreach (Microsoft.SharePoint.Client.File file in folder.Files)
            {
               string destinationfolder = Destination + "/" + folder.ServerRelativeUrl;
                if (file.Name.Contains(".mpp"))
                {
                    i++;
                    newName = newName + "-" + i.ToString() + ".mpp";
                    Stream fs = Microsoft.SharePoint.Client.File.OpenBinaryDirect(site, file.ServerRelativeUrl).Stream;
                    byte[] binary = ReadFully(fs);
                    if (!Directory.Exists(destinationfolder))
                    {
                        Directory.CreateDirectory(destinationfolder);
                    }
                    //int index = file.Name.IndexOf(".");
                    string oldName = file.Name;
                    FileStream filestream = new FileStream(destinationfolder + "/" + file.Name.Replace(oldName, newName), FileMode.Create);
                    int index = newName.IndexOf("-");
                    newName = newName.Substring(0, index);
                    BinaryWriter writer = new BinaryWriter(filestream);
                    writer.Write(binary);
                    writer.Close();
                }
                else continue;
                site.ExecuteQuery();
            }
        }
        public static byte[] ReadFully(Stream input)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }
    }
  • Please post your current code to can help you faster – Mohamed El-Qassas MVP Sep 5 '17 at 23:46
  • Added the code as requested. Please check and let me know what's going wrong. – agasthya Sep 6 '17 at 7:04
0

I was able to fix my solution using the msdn article below: https://msdn.microsoft.com/en-us/library/office/hh124553%28v=office.14%29.aspx?f=255&MSPPError=-2147217396

My issue was with the SP2010 using forms and windows authentication. hence I had to specify the hostheader as per the article explicitly for my code to work.

Regards Agasthya

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.