1
votes

I have created a custom webpart and deployed it in sharepoint. Now i want to modify the webpart and use the url of the site page where the web part is embedded. How can i access the url progmatically?

2
In the webpart codebehind?Kyle Muir
yes, in the C# code! @KyleMuirThunderstruck
See my answer below :)Kyle Muir

2 Answers

1
votes

If you mean in the Webpart codebehind you can reference it like this:

string currentWebUrl = SPContext.Current.Web.Url;

NOTE: SPContext.Current.Site\Web (unlike new SPWeb\SPSite) does NOT need to be disposed.

Hope this helps.

Resource:

1
votes

Something like this will give you access to the url and then you can do whatever your trying to do:

 using (var spSite = new SPSite(SPContext.Current.Web.Url))
 using (var spWeb = spSite.OpenWeb())
 {          
  // code here
 }