1

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.

  • 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
1

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 ?

0

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
      }
}
  • 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
0

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 :)

  • 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 Jafer 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 Jafer Jan 17 '13 at 12:43

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.