0

I am trying to add a hyperlink for a document which is uploaded in the document library. I don't want to hardcode the url by giving the specific sever url like http://xyz:1112/documents/abc.doc. I want to have a retrieve the sitecollection url.

3 Answers 3

2

In code-behind:

string fileUrl = SPUrlUtility.CombineUrl(SPContext.Current.Site.Url, "documents/foo.docx");

Or in .ascx file:

<a href='<%SPUrlUtility.CombineUrl(SPContext.Current.Site.Url, "documents/foo.docx")%>'>click</a>

(would need the proper Import declaration)

or you can use client-side script with the variable _spPageContextInfo.webServerRelativeUrl.

0

The code below will create a hyperlink using the relative URL of the current SharePoint site, and whatever sub-item you’d like to link to

<a href=\”‘+_spPageContextInfo.webServerRelativeUrl+’/Documents/abc.doc\” />
0

You can get the current url by using the SPContext.Current instance and working from there. For example:

// root site – eg. Web App url
string rootSiteCollectionURL = SPContext.Current.Web.Site.WebApplication.Sites[0].Url;

// site collection url
string SiteCollectionURL = SPContext.Current.Web.Site.Url;
2
  • I am trying to write it in ascx file for hyperlink <a href="xyz:1112/Documents/abc.tsv" target="_blank">. here i can't user spcontext right ??
    – mak
    Commented Jul 21, 2015 at 13:58
  • In the code behind file , you can retrive url and then push it to <a tags href /> . you can also use Javascript to achive this. Commented Jul 21, 2015 at 14:02

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.