Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-Site Get By Document Type #21

Open
technicallyerik opened this issue Mar 31, 2016 · 0 comments
Open

Multi-Site Get By Document Type #21

technicallyerik opened this issue Mar 31, 2016 · 0 comments
Milestone

Comments

@technicallyerik
Copy link

On multi-site instances you might want to get all documents by type, within the site you are currently in. We ended up writing an extension method like this. Worth adding into the core code?

/// <summary>
///     Gets all documents of type T within the current internationalized site
/// </summary>
/// <param name="context">Umbraco context</param>
public static IEnumerable<T> GetByDocumentTypeWithinSite<T>(this IUmbracoContext context)
{
    int rootNode = ResolveRootNodeId(context);

    // Get all documents under the home node with the specified document type
    // Document paths in the Umbraco cache are defined as "-1,<root node>,..."
    var documents = Vault.Context.QueryRelative<T>(
        XpathHelper.GetXpathForDocumentTypeUnderHomeNode(GetUmbracoEntityAliasFromType(typeof(T)), rootNode));

    return documents;
}
/// <summary>
///     Gets the Xpath query for a document type underneith a home node id
/// </summary>
/// <param name="documentTypeId">The document type</param>
/// <param name="homeNodeId">The home node id</param>
public static string GetXpathForDocumentTypeUnderHomeNode(string documentTypeId, int homeNodeId)
{
    return string.Format("//{0}[starts-with(@path, '-1,{1},')]", documentTypeId, homeNodeId);
}
@kensykora kensykora added this to the v1.3.0 milestone Mar 31, 2016
@kensykora kensykora changed the title [Suggestion] Multi-Site Get By Document Type Multi-Site Get By Document Type Mar 31, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants