0

My problem is simple. From an aspx page, I need to open in a new window a document stored in Sharepoint. To access the document in SharePoint I need to provide credentials. To open the document in a new window, I need to use javascript. => how to link the two ? here is the code :

    ClientContext ctx = new ClientContext(strServerUrl);
    Web currentWeb = ctx.Web;

    ctx.Load(currentWeb);
    ctx.Credentials = new System.Net.NetworkCredential("Login", "Password", "Domain");

    ctx.ExecuteQuery();

    // Here I have access to SharePoint. 
    // I can download the document, but I just want to display it in a new window

    // something is missing here

    string strScript;
    strScript = "window.open('" + myUrltotheDocument + "','','width=800,height=800,resizable=yes,scrollbars=yes');";
    ScriptManager.RegisterStartupScript(myPanel, myPanel.GetType(), "ShowInfo", strScript, true);

Thank you for your help.

2

2 Answers 2

1

Finally, the only way I found to open the document in a new window from a .aspx page, is to download the document in a folder on the server, and then, to open a window in javascript with a link to the downloaded document.

Uri uriVar = new Uri(strDocUrl);

int intDirectoryIndex = uriVar.LocalPath.LastIndexOf("/");
string strFileName = uriVar.LocalPath.Substring(intDirectoryIndex + 1);

System.Net.WebClient wcVar = new System.Net.WebClient();
wcVar.Credentials = new System.Net.NetworkCredential("Login", "Pwd", "Domain");
string strPath = HttpContext.Current.Server.MapPath("~/FileUpload/");
wcVar.DownloadFile(strDocUrl, strPath+strFileName);
string strLocalName = "FileUpload/" + strFileName;

string strScript;
strScript = "window.open('" + strLocalName + "','','width=800,height=800,resizable=yes,scrollbars=yes');";
ScriptManager.RegisterStartupScript(myPanel, myPanel.GetType(), "ShowInfo", strScript, true);

I do not consider this as a "nice solution", but it does the job... If someone has a better one, please tell me.

Thank you for the ones who took time to answer.

0

If the document is saved in a sharepoint library you can change the library settings so that the document, when clicked, is not downloaded but opened in a new browser window. Have you tried this?

http://office.microsoft.com/en-us/sharepoint-server-help/change-how-to-open-a-document-in-a-library-with-one-click-HA101729873.aspx

3
  • This is an interesting link. In my case, the user does not click directly on the link in SharePoint. The document is accessed by a .Net application, with .aspx pages.
    – Mister B.
    Commented Oct 17, 2013 at 21:58
  • Check if you have installed office web apps on the sharepoint server.
    – Skaft
    Commented Oct 18, 2013 at 9:28
  • Office web apps is not installed on the sharepoint server. We will install it. This, may be, can solve the problem for Office documents, but not for .pdf, jpg,...
    – Mister B.
    Commented Oct 21, 2013 at 21:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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