Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm learning SharePoint 2010 and facing a strange problem :

I implement a custom action to add a ribbon button under Documents tab, this button has a command to open a modal dialog in my site which is being located at http://sharepointserver/dev/ . The Url parameter of showModalDialog function is pointed to my application page : test.aspx. The code of test.aspx.cs as below:

     ......................
     protected void Page_Load(object sender, EventArgs e)
    {
        SPWeb web = this.Web;
            try
            {
                SPList list = web.Lists[this.TargetListId];
                SPListItem item = list.GetItemById(this.TargetItemId);
            }
    }

My problem here is this.Web always points to http://sharepointserver, not my current site http://sharepointserver/dev/. This results in list #notfound when I execute SPList list = web.Lists[this.TargetListId]

My Sharepoint project Site URL is http://sharepointserver/dev/ and Feature Scope is being set to Web so I don't know why this.Web doesn't want to give me the current site address. (SPContext.Current.Web also has a same problem)

Any help will be appreciated.

share|improve this question
    
Ok guys, I just created a new project with the same site collection and it worked fine, there must be some wrong configuration in my old project which I still don't know why. Anyways, thanks for all your help, really appreciate it :) –  davidcoder Jan 17 '13 at 15:19
add comment

3 Answers 3

is the link to your application page pointing to http://sharepointserver/dev/_layouts/test.aspx or is the link to your application page pointing to http://sharepointserver/_layouts/test.aspx

It should be the first if is the second this will explain your problem. if not then the question will be is dev/ as sub site or a managed path site collection ?

share|improve this answer
    
My Application page url is : sharepointserver/dev/_layouts/SharePointProject1/test.aspx . dev/ is a managed path site collection –  davidcoder Jan 17 '13 at 11:28
    
OK just created a site under an explicit managed path and an application page and this.Web is returning the right web see image link –  Renzo Jan 17 '13 at 13:29
add comment

pls use like this for current web...

SPWeb spweb = SPContext.Current.Web;

or

using (SPSite spsite=new SPSite(SPContext.Current.Web.Url))
{
      using (SPWeb spweb=spsite.OpenWeb())
      {
          // you can access current spweb object
      }
}
share|improve this answer
    
Your answer is correct, but he said, that it doesn't work for him as well. –  MikhailSP Jan 17 '13 at 10:31
    
try option from both of one... –  Hitesh Chandegara Jan 17 '13 at 10:41
    
from both of one option must be worked. –  Hitesh Chandegara Jan 17 '13 at 10:57
    
Both didn't work :( –  davidcoder Jan 17 '13 at 11:26
add comment

It is very strange that you cant use this:

 protected void Page_Load(object sender, EventArgs e)
{
        SPWeb web = SPContext.Current.Web;
        try
        {
            SPList list = web.Lists[this.TargetListId];
            SPListItem item = list.GetItemById(this.TargetItemId);
        }
        catch(exception a)
        {
        }
}

http://msdn.microsoft.com/en-gb/library/microsoft.sharepoint.spweb.aspx

but you can use this:

string url = HttpContext.Current.Request.Url
using (SPSite site = new SPSite(url))
{
    using (SPWeb web = site.OpenWeb())
    {
            try
            {
                SPList list = web.Lists[this.TargetListId];
                SPListItem item = list.GetItemById(this.TargetItemId);
            }
            catch(exception a)
            {
            }
    }
}

can you explain where your putting the code? as i can see page_load event (asp.net code behined) or webpart within feature or within feature reciver?

hope it helps :)

share|improve this answer
    
SPContext.Current.Web didn't work either :(. I'm putting the code in asp.net code behind –  davidcoder Jan 17 '13 at 11:25
    
ok iv ammended the second part (code) instead of context iv used the httpcontext :) –  ali Sharepoint Jan 17 '13 at 11:52
    
if it doesnt solve your issue than please debug your code by putting a break point at the url part to see where its comming from, the current context is probably correct in showing you that result as in that moment in time the current context is that url your being displayed ;) ! –  ali Sharepoint Jan 17 '13 at 12:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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