1

I want to read files from server drive inside SharePoint page using a Page Viewer web part.

Currently I am trying to add location of file in the web part but it is showing nothing (blank page). And when I copy paste the same location path in browser it opens the file(Images and PDF file only not other office files) and other office files are prompted with open and save options.

path: //xx.xx.x.xxx/Test/myPDF.pdf

tried this also

file://\\xx.xx.x.xxx/Test/myPDF.pdf
1

I would use the search service application and crawl the file share you need to include in SharePoint. When that is done, and the file share is crawled, you can use the Content Search Web part. The benefit is that you don’t have to know the exact location of your files, since they will be handled automatically by the Search Service Application.

0

Try

file:\\\\xx.xx.x.xxx\Test.myPDF.pdf

(altough i would not recommend such a solution at all, as it is not working with Clients which have Access to SharePoint, but not to the fileshare)

0

Just to get office files from remote shared drive inside SharePoint page i did these things.

private void EnumerateFiles()
    {
        try
        {
            using (NetworkShareAccesser.Access("ServerName", "Netelligent", "username", "password"))
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();

                sb.Append(String.Format("<ul style='margin-left:0.1rem' id='FileUL'>"));
                string filePath = FileLocation;






                string path = string.Empty;
                FileAttributes attr = File.GetAttributes(FileLocation);

                //detect whether its a directory or file
                if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                {
                    path = FileLocation;
                    DirectoryInfo dir = new DirectoryInfo(path);



                    foreach (FileInfo flInfo in dir.GetFiles())
                    {
                        string fext = flInfo.Extension;
                        string[] extensionArray = { ".doc", ".docx", ".ppt", ".pptx", ".pdf", ".xls", ".xlsx", ".rtf", ".txt" };
                        foreach (string x in extensionArray)
                        {
                            if (x.Contains(fext))
                            {
                                String name = flInfo.DirectoryName;
                                String filename = flInfo.Name;

                                filePath = fpath;
                                long size = flInfo.Length;
                                DateTime creationTime = flInfo.CreationTime;

                                sb.Append(String.Format("<li onclick=\"SavingDoc('" + creationTime + "','" + filename + "','" + filePath + "') \"><a>" + filename + "</a></li>"));


                            }
                        }
                    }
                }
                else
                {
                    path = remStrings;

                    DirectoryInfo dir = new DirectoryInfo(path);

                    foreach (FileInfo flInfo in dir.GetFiles())
                    {
                        string fext = flInfo.Extension;

                        string[] extensionArray = { ".doc", ".docx", ".ppt", ".pptx", ".pdf", ".xls", ".xlsx", ".rtf", ".txt" };
                        foreach (string x in extensionArray)
                        {
                            if (x.Contains(fext))
                            {

                                String name = flInfo.DirectoryName;
                                String filename = flInfo.Name;
                                if (filename == SingleFile)
                                {
                                    filePath = fpath;
                                    long size = flInfo.Length;
                                    DateTime creationTime = flInfo.CreationTime;

                                    sb.Append(String.Format("<li onclick=\"SavingDoc('" + creationTime + "','" + filename + "','" + filePath + "') \"><a>" + filename + "</a></li>"));


                                }
                            }
                        }
                    }
                }






                sb.Append(String.Format("</ul>"));
                ltFileName.Text = sb.ToString();



            }
        }
        catch (UnauthorizedAccessException)
        {
        }
    }

Then, using ajax call i saved the file inside document library and provide the url of document to iframe in the same page which will display the document, here we can provide file location by editing webpart in browser.

SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite oSite = new SPSite(SPContext.Current.Web.Url)) { using (SPWeb oWeb = oSite.OpenWeb()) { // Get the current user.

                         oSite.AllowUnsafeUpdates = true;
                         oWeb.AllowUnsafeUpdates = true;

                         SPFolder myLibrary = oWeb.Folders[documentLibraryName];

                         // Prepare to upload
                         Boolean replaceExistingFiles = true;
                         String fileName = System.IO.Path.GetFileName(filename);




                         FileStream fileStream;
                         bool isFile;
                         isFile = filepath.Contains(filename);
                         if (isFile)
                         {
                             fileStream = File.OpenRead(filepath);
                         }
                         else
                         {
                             filepath = filepath + "/" + filename;
                             fileStream = File.OpenRead(filepath);
                         }
                         // Upload document
                         SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);




                         if ((spfile.Level == SPFileLevel.Draft) && (spfile.CheckOutStatus == SPFile.SPCheckOutStatus.None))
                         {
                             spfile.Publish("File Automatically Published");

                             try
                             {
                                 if (spfile.Item.ModerationInformation.Status == SPModerationStatusType.Pending)
                                 {
                                     spfile.Approve("File Automatically published");
                                 }
                             }
                             catch (System.Exception)
                             { 
                             }

                             spfile.Update();
                         }


                         // Commit 
                         myLibrary.Update();
                     }
                 }
             });

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.