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 working on a custom 404 page for a new public facing SharePoint site which is replacing our current non-SharePoint site. Customers may have bookmarks that will break on the new site, simply because '/Pages' is not in their bookmark's URL.

So, I'm creating a C# user control which is loaded by a web part, and that web part is added to www.mydomain.com/Pages/pagenotfound.aspx. The user control reads a querystring value (url) for the bookmarked page and replaces each '/' char, in turn, with /Pages/, and then checks to see if that page exists, using the following method:

protected bool checkPageExists(string url)
{
    bool returnedBool = false;
    SPSite site = SPContext.Current.Site;
    SPWeb web = SPContext.Current.Web;

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite ElevatedSite = new SPSite(site.ID))
        {
            using (SPWeb ElevatedWeb = ElevatedSite.OpenWeb(web.ID))
            {
                SPFile page = web.GetFile(url);
                if (page.Exists)
                    returnedBool = true;
                else
                    returnedBool = false;
            }
        }
    });
    return returnedBool;
}

let's say our bookmarked URL (without domain) is /us/training/default.aspx.

I loop through this string, changing each '/', one at a time, and then calling 'checkPageExists(url)' above.

the first two runs through work fine, for:

  • /Pages/us/training/default.aspx
  • /us/Pages/training/default.aspx

but the third possible replacement...

  • /us/training/Pages/default.aspx

...gets to the if (page.Exists) line of my method above and then redirects the browser to my sites sign in page

I should point out that none of these 3 possible replacements to the URL will end up returning true for page.Exists, as I've not created a US training page yet. However, the result of my 404 page should be along the lines of "we tried to find your page, but failed", rather than asking the user to sign in.

Any ideas why this third replacement should prompt for a sign in? I thought that by running with elevated privileges, I'd get around any user permission issues, but apparently not.

Any help would be greatly appreciated.

(also, feel free to update the tags for the question, I have such as hard time figuring out the best ones to use)

Kevin

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

I don't know if this could be the cause of your problem, but by looking at your code I noticed that you use the istruction SPFile page = web.GetFile(url);

If this is the case (and it isn't a simple copy&paste error) be aware that you are breaking the elevation process. By looking at your code I would assume that you already know this, but you should be using the elevated web istance, ElevatedWeb. Could you try and see if your problem is resolved by fixing your code so that the elevation context should be valid?

using (SPWeb ElevatedWeb = ElevatedSite.OpenWeb(web.ID))   
        {   
            SPFile page = ElevatedWeb.GetFile(url);   
            if (page.Exists)   
                returnedBool = true;   
            else   
                returnedBool = false;   
        }  
share|improve this answer
    
/facepalm! You're absolutely right. It wasn't a typo in my question, but it was a typo in my code. Using ElevatedWeb when finding the page stopped me from being prompted to sign in. Why? who knows! I'd have thought that would prompt me to log in for all the ElevatedWeb values (/Pages/us/training, /us/Pages/training, etc..) but it didn't. However, I tested this with the URL I gave earlier, and I get my "couldn't find the page at all" error, and with a URL that does work, and it still worked. Thanks a million :) –  QMKevin Oct 14 '11 at 15:12
    
I --suppose-- (also know as 'wild guess') that the first two urls were discarded before requiring authentication while the last was processed to a further level by the .Exist logic. Maybe the first two urls didn't make sense in your site collection so SharePoint decided to discard them and return false without further processing? Guess I will try to have a look with DotPeek when I have the time - I will update the answer if I discover something. For now, I'm simply glad that you were able to resolve the issue. –  SPArchaeologist Oct 14 '11 at 15:19
    
I'd certainly be interested to hear if you find anything about a possible cause, but I wouldn't knock yourself out over it, just for me ;) Thanks again.. I spent the last 6 horus trying to figure this out. I should have come to sharepoint.stackexchange.com first! (no, I shouldn't.. or I'd be posting every little thing, and no-one wants that) –  QMKevin Oct 14 '11 at 15:22
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.