TransWikia.com

Error while reading the file in SharePoint Document Library

SharePoint Asked on March 8, 2021

I have developed C# console application using csom to read all the files (excel, doc, slide, pdf) in the SharePoint Document Library and converts to memory stream and retrieved if any external links are used in the files. The application is working as expected when tested in my local machine

When I deploy the code to the server and tested another site “http://edms.com.sg:2100/sites/FD/EDMS“, I am getting the below error:

“Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException:
The property or field ‘Length’ has not been initialised. It has not
been requested or the request ha not been executed. It may need to be
explicitly requested.”

enter image description here

Below is function code

namespace ExScan_EDMS
{
    class Program
    {
        public static string siteUrl = "";
        public static string _logFileName = "";
        public static DateTime _startTime, _endTime;
        public static uint _totalFileCounter = 0;
        public static uint _totalFileWithLinks = 0;
        public static uint _totalFileWithPassword = 0;
        public static bool _promptForPassword = false;
        public static string _ignorePathContaining = "~snapshot";
        public static string ReplaceKeyWord1 = ConfigurationSettings.AppSettings["ReplaceKeyWord1"].ToString();
        public static string ReplaceKeyWord2 = ConfigurationSettings.AppSettings["ReplaceKeyWord2"].ToString();
        public static string _title = "";
        public static List<string> subfolderslst;


        static void Main(string[] args)
        {

            String FullURL = "";

            Console.ForegroundColor = ConsoleColor.Blue;
            string displayName = String.Format("|    ExScan_EDMS - Office File Scanner - {0}   |", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
            Console.WriteLine(new String('=', displayName.Length));
            Console.WriteLine(displayName);
            Console.WriteLine(new String('=', displayName.Length));
            Console.ResetColor();

            try
            {
               FullURL = args[0];
            }
            catch
            {
                Console.WriteLine("Please specify path: rn e.g. ExScan.exe "site url"");
                return;
            }

            List<string> siteList = GetSitesFromURL(FullURL); //try to get the sharepoint site

            if (siteList.Any())
            {
                siteUrl = siteList[siteList.Count() - 1]; //get the last url as the site
            }
            else
            {
                Console.WriteLine("Unable to find a valid site URL");
            }

            _startTime = DateTime.Now;

            _logFileName = String.Format("log_{0}.csv", DateTime.Now.ToString("yyyyMMdd-HHmmss"));

            Console.WriteLine("main url {0}", siteUrl);
            using (ClientContext clientContext = new ClientContext(siteUrl))
            {
                Log.WriteHeader();

                string path = string.Empty;
                path = FullURL.Replace(siteUrl, "").Trim();

                String[] folderdetails = path.Split('/');

                int cnt = 0;
                subfolderslst = new List<string>();
                foreach (string s in folderdetails)
                {
                    cnt++;
                    if (cnt == 1)
                        _title = s.ToString().Trim();
                    if (cnt > 1)
                        subfolderslst.Add(s.ToString().Trim());
                }

                clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
                clientContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

                Web web = clientContext.Web;
                var list = clientContext.Web.Lists.GetByTitle(_title);
                //var rootFolder = list.RootFolder;

                clientContext.Load(web, website => website.Title, website => website.HasUniqueRoleAssignments);
                clientContext.Load(clientContext.Web, w => w.Lists, w => w.Title);
                clientContext.Load(list);

                if (subfolderslst.Count == 0)
                {
                    FolderCollection Mainfolderscol = list.RootFolder.Folders;

                    clientContext.Load(Mainfolderscol);
                    clientContext.ExecuteQuery();

                    foreach (Folder MainFolder in Mainfolderscol)
                    {
                        GetFoldersAndFiles(MainFolder, clientContext);
                    }
                }

                else if (subfolderslst.Count > 0)
                {
                    FolderCollection folderscol = list.RootFolder.Folders;
                    clientContext.Load(folderscol);
                    clientContext.ExecuteQuery();

                    String FolderName = string.Empty;
                    if (subfolderslst.Count == 1)
                    {
                        FolderName = subfolderslst[subfolderslst.Count - 1].ToString();
                    }
                    else
                    {
                        FolderName = subfolderslst[subfolderslst.Count - 2].ToString();
                    }

                    foreach (Folder subFolder in folderscol)
                    {
                        if (subFolder.Name == FolderName)
                        {
                            if (subfolderslst.Count == 1)
                            {
                                if (subFolder.Name == subfolderslst[subfolderslst.Count - 1].ToString())
                                {
                                    GetFoldersAndFiles(subFolder, clientContext);
                                }
                            }
                            else if (subfolderslst.Count > 1)
                            {
                                FolderCollection folderscol1 = subFolder.ListItemAllFields.Folder.Folders;
                                clientContext.Load(folderscol1);
                                clientContext.ExecuteQuery();
                                foreach (Folder subFolder1 in folderscol1)
                                {
                                    if (subFolder1.Name == subfolderslst[subfolderslst.Count - 1].ToString())
                                    {
                                        GetFoldersAndFiles(subFolder1, clientContext);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            _endTime = DateTime.Now;
            string endResult = String.Format("rnrnSUMMARY:rnStart: {0}rnEnd: {1}rnTotal Files: {2}rnFiles with Links: {3}rnFiles with Passwords: {4}rnLog File: {5}", _startTime.ToString("dd-MM-yyyy HH:mm:ss"), _endTime.ToString("dd-MM-yyyy HH:mm:ss"), _totalFileCounter.ToString(), _totalFileWithLinks.ToString(), _totalFileWithPassword.ToString(), _logFileName);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(endResult);
            Console.ResetColor();
            Log.WriteAnythingToLog(endResult);
            Console.ReadKey();
        }


        private static void GetFoldersAndFiles(Folder mainFolder, ClientContext clientContext)
        {
            Console.WriteLine("inside GetFoldersAndFiles method");
            clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
            clientContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            clientContext.Load(mainFolder, k => k.Files, k => k.Folders);
            clientContext.ExecuteQuery();
            Console.WriteLine("after credentialss");

            foreach (var folder in mainFolder.Folders)
            {
                GetFoldersAndFiles(folder, clientContext);
            }

            foreach (var file in mainFolder.Files)
            {

                try
                {
                    var fileRef = file.ServerRelativeUrl;
                    var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);

                    var serverRelativeUrl = fileRef;


                    var fileurl = new Uri(clientContext.Url)
                      .GetLeftPart(UriPartial.Authority) + serverRelativeUrl;

                    using (var objMemory = new System.IO.MemoryStream())
                    {
                        byte[] buffer = new byte[1024 * 64];
                        int iRead = 0;

                        while ((iRead = fileInfo.Stream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            objMemory.Write(buffer, 0, iRead);
                        }

                        objMemory.Seek(0, System.IO.SeekOrigin.Begin);

                        ProcessFile(objMemory, file.Name, fileurl, file.TimeCreated, file.TimeLastModified, file.Length, "");


                    }
                }
                catch (Exception ex)
                {
                    string e = ex.Message;
                    Console.WriteLine("inside objMemory err msg: {0}", e);

                    continue;

                }
            }
        }

        public static void ProcessFile(System.IO.MemoryStream objMemory, string fileName, string fileurl, DateTime fileTimeCreated, DateTime fileTimeLastModified, long fileLength, string WorkbookPassword = "")
        {

            //Set license
            Aspose.Cells.License license = new Aspose.Cells.License();
            license.SetLicense("Aspose.Total.lic");

            //Use when path is too long and file had to be copied locally to be analyzed
            string newLocalPath = String.Empty;
            string FileName = String.Empty;


            string extension = string.Empty;
            int fileExtPos = fileName.LastIndexOf(".", StringComparison.Ordinal);
            if (fileExtPos >= 0)
                extension = fileName.Substring(fileExtPos, fileName.Length - fileExtPos);

            LogEntry logEntry = new LogEntry();
            FileName = fileName;

            if (FileName.Contains(ReplaceKeyWord1))
            {
                FileName = FileName.Replace(ReplaceKeyWord1, "");
            }
            else if (FileName.Contains(ReplaceKeyWord2))
            {
                FileName = FileName.Replace(ReplaceKeyWord2, "");
            }
            logEntry.Filename = FileName.Trim(); //Path.GetFileName(path);
            logEntry.Extension = extension;
            logEntry.Path = fileurl.Replace(FileName, "").Trim();
            logEntry.Created = fileTimeCreated;
            logEntry.Modified = fileTimeLastModified;
            logEntry.Accessed = fileTimeLastModified;
            logEntry.FileSize = fileLength;

            try
            {
                Aspose.Cells.LoadOptions options = new Aspose.Cells.LoadOptions();

                if (!String.IsNullOrEmpty(WorkbookPassword)) //load password
                {
                    options.Password = WorkbookPassword;
                    logEntry.HasPassword = true;
                    _totalFileWithPassword++;
                }

                if (extension != null && (extension.ToLower() == ".xls" || extension.ToLower() == ".xlsx" || extension.ToLower() == ".xlsm"))
                {
                    using (Workbook workbook = new Workbook(objMemory, options))
                    {
                        if (workbook.Settings.IsWriteProtected)
                        {
                        }

                        //Macro Handling
                        logEntry.HasMacro = workbook.HasMacro;
                        bool hasMacroButNoLinksInMacro = true;

                        if (logEntry.HasMacro)
                        {
                            VbaModuleCollection modules = workbook.VbaProject.Modules;

                            for (int i = 0; i < modules.Count; i++)
                            {
                                VbaModule module = modules[i];
                                //Console.WriteLine("Module: {0}", module.Name);
                                String code = module.Codes;
                                String pattern = @"(w:\[w \]+[.][w]+)"; //match local path

                                foreach (var matchedPath in Regex.Matches(code, pattern, RegexOptions.IgnoreCase))
                                {
                                    logEntry.MacroModule = module.Name;
                                    logEntry.MacroLinks = matchedPath.ToString();
                                    Log.WriteToLog(logEntry);
                                    Console.WriteLine("Module: {0} Local Path: {1}", module.Name, matchedPath.ToString());
                                    hasMacroButNoLinksInMacro = false;
                                }

                                pattern = @"([\]{2}[w \]+[.][w]+)"; //match network path

                                foreach (var matchedPath in Regex.Matches(code, pattern, RegexOptions.IgnoreCase))
                                {
                                    logEntry.MacroModule = module.Name;
                                    logEntry.MacroLinks = matchedPath.ToString();
                                    Log.WriteToLog(logEntry);
                                    Console.WriteLine("Module: {0} Remote Path: {1}", module.Name, matchedPath.ToString());
                                    hasMacroButNoLinksInMacro = false;
                                }
                                //Replace the original message with the modified message
                                //if (code.Contains("This is test message."))
                                //{
                                //    code = code.Replace("This is test message.", "This is Aspose.Cells message.");
                                //    module.Codes = code;
                                //}
                            }
                        }

                        logEntry.MacroLinks = ""; //clear
                        logEntry.MacroModule = "";

                        if (workbook.HasExernalLinks()) //Process those with external links
                        {
                            //Increment File With Links Counter
                            _totalFileWithLinks++;

                            var links = workbook.Worksheets.ExternalLinks;

                            for (int i = 0; i < links.Count; i++)
                            {
                                logEntry.Links = links[i].DataSource;
                                logEntry.LinksReferred = links[i].IsReferred;
                                Log.WriteToLog(logEntry);
                            }

                        }
                        else //Process those without external inks
                        {
                            if (!logEntry.HasMacro)
                                Log.WriteToLog(logEntry);
                            else
                            {
                                //File has macro but no links were found
                                if (hasMacroButNoLinksInMacro)
                                {
                                    logEntry.MacroLinks = ""; //clear
                                    logEntry.MacroModule = "";
                                    Log.WriteToLog(logEntry);
                                }
                            }
                        }
                    }

                    //Increment Total File Counter
                    _totalFileCounter++;
                }
                else if (extension != null && (extension.ToLower() == ".doc" || extension.ToLower() == ".docx"))
                {
                    Aspose.Words.Document doc = new Aspose.Words.Document(objMemory);
                    uint _hlinkcnt = 0;
                    foreach (Aspose.Words.Fields.Field field in doc.Range.Fields)
                    {
                        if (field.Type == Aspose.Words.Fields.FieldType.FieldHyperlink)
                        {
                            FieldHyperlink link = (FieldHyperlink)field;
                            logEntry.Links = link.Address;
                            logEntry.LinksReferred = false;
                            Log.WriteToLog(logEntry);
                            _hlinkcnt++;
                        }
                    }

                    if (_hlinkcnt > 0)
                        _totalFileWithLinks++;
                    else
                        Log.WriteToLog(logEntry);

                    //Increment Total File Counter
                    _totalFileCounter++;
                }
                else if (extension != null && (extension.ToLower() == ".ppt" || extension.ToLower() == ".pptx"))
                {
                    Presentation pres = new Presentation(objMemory);
                    uint _hlinkcnt = 0;
                    //Get the hyperlinks from presentation slides
                    foreach (ISlide slide in pres.Slides)
                    {
                        IList<IHyperlinkContainer> links = slide.HyperlinkQueries.GetAnyHyperlinks();

                        if (links.Count > 0)
                            _hlinkcnt++;

                        foreach (IHyperlinkContainer link in links)
                        {
                            logEntry.Links = link.HyperlinkClick.ExternalUrl;
                            Log.WriteToLog(logEntry);
                        }
                    }

                    if (_hlinkcnt > 0)
                        _totalFileWithLinks++;
                    else
                        Log.WriteToLog(logEntry);

                    //Increment Total File Counter
                    _totalFileCounter++;
                }
                else if (extension != null && extension.ToLower() == ".pdf")
                {
                    //Open document
                    Aspose.Pdf.Document document = new Aspose.Pdf.Document(objMemory);

                    uint _hlinkcnt = 0;
                    // Traverse through all the page of PDF
                    foreach (Aspose.Pdf.Page page in document.Pages)
                    {
                        // Get the link annotations from particular page
                        Aspose.Pdf.InteractiveFeatures.Annotations.AnnotationSelector selector = new Aspose.Pdf.InteractiveFeatures.Annotations.AnnotationSelector(new Aspose.Pdf.InteractiveFeatures.Annotations.LinkAnnotation(page, Aspose.Pdf.Rectangle.Trivial));

                        page.Accept(selector);
                        // Create list holding all the links
                        System.Collections.IList list = selector.Selected;
                        // Iterate through invidiaul item inside list
                        foreach (Aspose.Pdf.InteractiveFeatures.Annotations.LinkAnnotation a in list)
                        {
                            // Print the destination URL
                            _hlinkcnt++;
                            logEntry.Links = (a.Action as Aspose.Pdf.InteractiveFeatures.GoToURIAction).URI;
                            Log.WriteToLog(logEntry);
                        }
                    }

                    if (_hlinkcnt > 0)
                        _totalFileWithLinks++;
                    else
                        Log.WriteToLog(logEntry);

                    //Increment Total File Counter
                    _totalFileCounter++;
                }
                Console.WriteLine("Processed file {0}: '{1}'.", _totalFileCounter, fileurl);
            }
            catch (Exception e)
            {

                //Process those with errors
                if (new Regex(@"Pleasesprovidespassword").IsMatch(e.Message) || e.Message.ToLower().Contains("password")) //Password required
                {
                    if (_promptForPassword) //if Y was set as 2nd input variable
                    {
                        SecureString password = PasswordServices.PromptPassword(fileurl);
                        //call to process the file again
                        /////ProcessFile(path, SecureClass.ToInsecureString(password));

                        //Log.WriteToLog(logEntry);
                        //Console.WriteLine("Processed file '{0}'.", path);
                    }
                    else
                    {
                        logEntry.Remarks += e.Message;
                        logEntry.HasPassword = true;
                        _totalFileWithPassword++;
                        Log.WriteToLog(logEntry);

                        //Increment Total File Counter
                        _totalFileCounter++;

                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Skipped passworded file {0}: '{1}'.", _totalFileCounter, fileurl);
                        Console.ResetColor();
                    }
                }
                else
                    if (new Regex(@"Invalidspassword").IsMatch(e.Message)) //Password entered is invalid
                    {
                        if (_promptForPassword) //ask for password again
                        {
                            _totalFileWithPassword--; //deduct file count due to invalid password

                            if (!String.IsNullOrEmpty(WorkbookPassword))
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("Password is invalid");
                                Console.ResetColor();

                                SecureString password = PasswordServices.PromptPassword(fileurl);
                                //call to process the file again
                                ////ProcessFile(fileurl, SecureClass.ToInsecureString(password));
                            }
                        }
                    }
                    else
                    {
                        _totalFileCounter++;
                        logEntry.Remarks += e.Message;
                        Log.WriteToLog(logEntry);
                        Console.WriteLine("Processed file {0}: '{1}'.", _totalFileCounter, fileurl);
                    }
            }
        }

        /// <summary>
        /// GetSitesFromURL - Used to get the site uri
        /// </summary>
        /// <param name="SiteURL"></param>
        /// <returns></returns>
        public static List<string> GetSitesFromURL(string SiteURL)
        {
            Uri uri = new Uri(SiteURL);
            string strRootURL = String.Format("{0}://{1}", uri.Scheme, uri.Authority);
            string strBuildupPath = String.Empty;
            List<string> possibleSites = new List<string>();


            foreach (var s in uri.Segments)
            {
                try
                {
                    strBuildupPath += s;

                    string strSiteURL = String.Format("{0}{1}", strRootURL, strBuildupPath);

                    using (ClientContext ctx = new ClientContext(strSiteURL))
                    {
                        Web web = ctx.Web;
                        ctx.Load(web);
                        ctx.ExecuteQuery();
                    }

                    possibleSites.Add(strSiteURL);
                    Console.WriteLine("URL: {0}", strSiteURL);

                }
                catch (Exception ex)
                {
                    string e = ex.Message;
                    Console.WriteLine("Error: {0}", e);
                    continue;

                }
            }
            return possibleSites;
        }
}

Kindly assist to resolve the error.

2 Answers

Finally I found the reason

The file.Length property is available only on SP 2013 site. When I tried to run my application on SP 2010 site I am getting the exception because the SP 2010 doesn't have file.Length property

Refer:File Properties - SharePoint 2010

Answered by Peter on March 8, 2021

First debug it and get to the line where the error is occurring. As far I see

the problem you are having is that your site path is not specified correctly.

Your FullUrl = @"http://servername/sites/xyz/default.aspx"; instead try

string FullUrl = @"http://servername/";

Answered by Ankit Kumar on March 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP