Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ben Norman 167 posts 276 karma points
    Apr 22, 2018 @ 21:20
    Ben Norman
    0

    Find / Convert DocTypes without losing data

    Is there a plugin that will tells me where in the content tree a doctype is used and gives me a link to it? My ultimate objective is to find a way to convert all doctypes of one type to another doctype programatically without losing data? Any suggestions on how to do that as well?

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 23, 2018 @ 06:43
    Michaël Vanbrabandt
    0

    Hi Ben,

    when you are in the content section and select one of the content nodes, you have a tab called Info. In this tab you have a container which looks like this:

    enter image description here

    There you can find the used Document Type which also has a link to it.

    Hope this helps!

    /Michaël

  • Steve Morgan 1349 posts 4458 karma points c-trib
    Apr 25, 2018 @ 15:31
    Steve Morgan
    0

    Hi Ben,

    My approach is a little bit hacky.. but create a test doc type and in the view add this script (probably a good idea to secure this page and / or delete it after).

    It loops through all nodes, does a count by doctype. If you set the doctype you're interested in on the line that declares docTypeSearchNodes then you'll get a link to each instance of that doc type.

    To convert - depends how many of these you have. There is an option in the back office to change doc type and map the properties though I'd probably do it in code using the content service.

    @{
        var umbracoHelper = new Umbraco.Web.UmbracoHelper(UmbracoContext.Current);
    
        // either set your home node ID or just use the first typed content at root for simple single homepage sites
        var homeNode = Umbraco.TypedContentAtRoot().First();
    
        var allNodes = homeNode.Descendants();
        var docTypeCount = new Dictionary<string, int>();
    
        var docTypeSearchNodes = homeNode.Descendants().Where(x => x.DocumentTypeAlias == "contentPage");  @* change to your doc type alias*@
    }
    
    <table>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>DocType</th>
            <th>Url</th>
        </tr>
        @foreach (var curNode in allNodes)
        {
            // increment the count if in dictionary or add new key if not
            int currentCount;
            docTypeCount.TryGetValue(curNode.DocumentTypeAlias, out currentCount);
            docTypeCount[curNode.DocumentTypeAlias] = currentCount + 1;
    
            @* comment this table out or it will show you all nodes *@
            @* <tr>
                <td><a href="/umbraco#/content/content/edit/@curNode.Id" target="_blank">@curNode.Id</a></td>
                <td>@curNode.Name</td>
                <td>@curNode.DocumentTypeAlias</td>
                <td><a href="@curNode.Url" target="_blank">@curNode.Url</a></td>
            </tr>
            *@
        }
    </table>
    
    <hr />
    <br />
    @* output the counts for each doc type *@
    @{
        <table>
            <tr>
                <th>Id</th>
                <th>Name</th>
            </tr>
            @foreach (var curKey in docTypeCount)
            {
                <tr>
                    <td>@curKey.Key</td>
                    <td>@curKey.Value</td>
                </tr>
            }
        </table>
    }
    
    <hr />
    
    
    @* output the nodes with links for the doc type we're looking for *@
    <table>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>DocType</th>
            <th>Url</th>
        </tr>
        @foreach (var curNode in docTypeSearchNodes)
        {
            <tr>
                <td><a href="/umbraco#/content/content/edit/@curNode.Id" target="_blank">@curNode.Id</a></td>
                <td>@curNode.Name</td>
                <td>@curNode.DocumentTypeAlias</td>
                <td><a href="@curNode.Url" target="_blank">@curNode.Url</a></td>
            </tr>
        }
    </table>
    
Please Sign in or register to post replies

Write your reply to:

Draft