5

I created a new screen in sitecore and added a button. On clicking of this button I am calling a method. Everything works fine except following piece of code.

if (sitecoreItem.Visualization.Layout != null) 
    {
    // code
    }

sitecoreItem.Visualization.Layout is throwing StackTrace:

" at System.Web.UI.Page.get_Request()\r\n at Sitecore.UIUtil.IsWebkit()\r\n at Sitecore.UIUtil.SupportsInlineEditing()\r\n at Sitecore.Sites.SiteContext.SetDisableWebEditEditing(String value)\r\n at Sitecore.Sites.SiteContext..ctor(SiteInfo siteInfo, Boolean processQueryString)\r\n at Sitecore.Sites.DefaultSiteContextFactory.GetSiteContext(String name)\r\n at Sitecore.Links.LinkProvider.PreviewLinkBuilder.ResolveTargetSite(Item item)\r\n at Sitecore.Links.LinkProvider.GetPreviewSiteContext(Item item)\r\n at Sitecore.Data.Items.ItemVisualization.ResolveContextDevice()\r\n at Sitecore.Data.Items.ItemVisualization.get_Layout()"

But when I call the same method from a Sitecore Agent it is working fine. Is there any configuration settings I am missing here?

Updated: It is an anchor link

<a href="#" class="btn btn-success js-import
                                        <%# ((TargetLanguage)Container.DataItem).LanguageTranslatedStatus.ToLower() == "delivered" || ((TargetLanguage)Container.DataItem).LanguageTranslatedStatus.ToLower() == "approved" || ((TargetLanguage)Container.DataItem).LanguageTranslatedStatus.ToLower() == "in_review" ?  "" : "disabled" %>"
                                        data-projectid="<%= Project.ProjectId %>" data-lang="<%# Eval("LanguageCode") %>">Import
                                                <span class="glyphicon glyphicon-cloud-upload"></span>
                                    </a>

call to the method

[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static object ImportLanguage(string projectId, string lang)
        {
            try
            {
                var sitecoreService = new SitecoreService();
                return sitecoreService.ImportLanguage(projectId, lang);
            }
            catch (Exception ex)
            {

                throw;
            }
        }

Full Stack:

Failed to import language {"Message":"Request is not available in this context","StackTrace":" at System.Web.UI.Page.get_Request()\r\n at Sitecore.UIUtil.IsWebkit()\r\n at Sitecore.UIUtil.SupportsInlineEditing()\r\n at Sitecore.Sites.SiteContext.SetDisableWebEditEditing(String value)\r\n at Sitecore.Sites.SiteContext..ctor(SiteInfo siteInfo, Boolean processQueryString)\r\n at Sitecore.Sites.DefaultSiteContextFactory.GetSiteContext(String name)\r\n at Sitecore.Links.LinkProvider.PreviewLinkBuilder.ResolveTargetSite(Item item)\r\n at Sitecore.Links.LinkProvider.GetPreviewSiteContext(Item item)\r\n at Sitecore.Data.Items.ItemVisualization.ResolveContextDevice()\r\n at Sitecore.Data.Items.ItemVisualization.get_Layout()\r\n at xxxxx.xxxx.xxx.xxx.xxxx.xxxxx(String itemGuid, String lang) in E:\\MyFolder\\xxxx.xxx.xxx\\xxx\\SitecoreService.cs:line 643\r\n at xxxxx.xxxx.xxxx.xxxx.SitecoreService.ImportLanguage(String projectId, String lang) in E:\\MyFolder\\xxxx.xxx.xxx\\xxx\\SitecoreService.cs:line 785\r\n at xxxxx.xxx.xxx.xxx.xxxx.ImportLanguage(String projectId, String lang) in E:\\MyFolder\\xxxx.xxx.xxx\\xxxxx\\ProjectDetails.aspx.cs:line 83","ExceptionType":"System.Web.HttpException"}
0
1

In short, your [WebMethod] doesn't run in the context of a Sitecore Site. The reason it works when run from an Agent is; it runs in the context of the "scheduler" site.

You can force a site context, with this change:

using (new SiteContextSwitcher("website") {
    var sitecoreService = new SitecoreService();
    return sitecoreService.ImportLanguage(projectId, lang);
}

But it isn't the best or most flexible approach. Likely someone can outline a better example.

Further reading: Correctly switching Sitecore contextes

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.