Kamran
Kamran

Reputation: 4100

Sitecore web API: Get the HTML content from Sitecore

I would like to get HTML of sitecore item via javascript.

I read about Sitecore Web API and while reading found details like there is "GetRenderingHtml" action available in Sitecore Web API.

I am unable to find the renderingid (highlighted in the below URL).

The sample API url looks like this.

http:///-/item/v1/-/actions/GetRenderingHtml?sc_database=master&language=en&reneringId=&sc_itemid=item-id

Any thoughts from you guys are highly appreciated!

Thanks

Upvotes: 1

Views: 765

Answers (1)

Vlad Iobagiu
Vlad Iobagiu

Reputation: 4118

The "GetRenderingHtml" Item Web API action was designed to work only with XSL renderings and not with stand-alone C# components like sublayout (".ascx" user controls) or layouts (".aspx", ".cshtml" pages).

You can see from the implementation of the "Sitecore.ItemWebApi.Actions.GetRenderingHtmlAction" class using a decompiler that it just runs the "getRenderingPreview" pipeline with specified arguments.

public override void Process(HttpContext httpContext)
{
    Assert.ArgumentNotNull(httpContext, "httpContext");
    httpContext.Response.Clear();
    httpContext.Response.DisableCaching();
    if (!this.IsAccessAllowed())
    {
    httpContext.Response.StatusCode = 0x193;
    httpContext.Response.End();
    }
    else
    {
    string previewHtml = RenderingPreviewProvider.GetPreviewHtml();
    httpContext.Response.ContentType = "text/html";
    httpContext.Response.Write(previewHtml);
    httpContext.Response.Flush();
    }
}

GetPreviewHtml looks like:

 public static string GetPreviewHtml()
 {
    Database database = GetDatabase();
    Language language = Context.Language;
    Item renderingItem = GetRenderingItem(database, language);
    Item sourceItem = GetSourceItem(database, language);
    string parameters = GetParameters();
    RenderingReference reference = new RenderingReference(renderingItem) {
    Settings = { 
        DataSource = sourceItem.ID.ToString(),
        Parameters = parameters
       }
    };
    GetRenderingPreviewArgs args = new   GetRenderingPreviewArgs(renderingItem, sourceItem);
    CorePipeline.Run("getRenderingPreview", args);
    return args.Result;
}

Upvotes: 1

Related Questions