2
db.Add(new DbTemplate("t1", t1_TemplateId));
db.Add(new DbTemplate("T", T_TemplateId)
        {
            BaseIDs = new[] { t1_TemplateId}
        });

db.Add(new DbItem("root")
{
    new DbItem("child1")
    {
        new DbItem("siteRoot")
        {
            new DbItem("Home")
            {
                new DbItem("Article")
            },
            new DbItem("Data")
            {
                new DbItem("theItem", ID.NewID, T_TemplateId)
                {
                    new DbLinkField("theField")
                    {
                        Anchor = "anchor",
                        Class = "class",
                        LinkType = "linktype",
                        QueryString = "querystring",
                        Target = "target",
                        TargetID = targetid,
                        Text = "text",
                        Title = "title",
                        Url = "url",
                    },
                    new DbLinkField("otherfield")
                }
            }

        }
    }
});

And then I try to get the values:

Item theItem = db.GetItem("/sitecore/content/root/child1/siteRoot/Data/theItem");
LinkField theField = theItem.Fields["theField"];

All attributes defined are missing...

What am I doing wrong?

4
  • Which version FakeDb/Sitecore do you use? Commented Dec 15, 2016 at 13:33
  • Sitecore 8.1 Initial Release, Sitecore.FakeDb 1.3.1
    – jgondev
    Commented Dec 15, 2016 at 13:46
  • Don't you have the same issue when you are using real Sitecore items? Commented Dec 15, 2016 at 13:54
  • When I have an item and I get a LinkField is working perfectly. But I want to mock that item and there is where the problem appears.
    – jgondev
    Commented Dec 15, 2016 at 14:05

3 Answers 3

3

Ok I finally found my mistake. Since I am using DbTemplates, the DbLinkField needs to be added to the template first.

db.Add(new DbTemplate("t1", t1_TemplateId)){
    new DbLinkfield("theField")
});
db.Add(new DbTemplate("T", T_TemplateId)
{
    BaseIDs = new[] { t1_TemplateId}
});

db.Add(new DbItem("root")
{
    new DbItem("child1")
    {
        new DbItem("siteRoot")
        {
            new DbItem("Home")
            {
                new DbItem("Article")
            },
            new DbItem("Data")
            {
                new DbItem("theItem", ID.NewID, T_TemplateId)
                {
                    new DbLinkField("theField")
                    {
                        Anchor = "anchor",
                        Class = "class",
                        LinkType = "linktype",
                        QueryString = "querystring",
                        Target = "target",
                        TargetID = targetid,
                        Text = "text",
                        Title = "title",
                        Url = "url",
                    },
                    new DbLinkField("otherfield")
                }
            }

        }
    }
});
0

For me the following code is working, maybe the Db context is missing in your code?

using (var db = new Db
{
    new DbItem("theItem", ID.NewID)
    {
        new DbLinkField("theField")
        {
            Anchor = "anchor",
            Class = "class",
            LinkType = "linktype",
            QueryString = "querystring",
            Target = "target",
            TargetID = ID.NewID,
            Text = "text",
            Title = "title",
            Url = "url",
        }
    }
})
{
    var item = db.GetItem("/sitecore/content/theItem");
    LinkField field = item.Fields["theField"];
}
3
  • This is weird. This code isolated is working good for me, but if the item structure is more complex then is not working anymore...
    – jgondev
    Commented Dec 15, 2016 at 14:09
  • Hm, let's post the the more complex structure into your question. Commented Dec 15, 2016 at 14:15
  • I've edited the question with the exact structure
    – jgondev
    Commented Dec 15, 2016 at 14:30
0

May sound like a silly question, but have you added your item to the fake db? The code below is working fine on my end:

var item = new Sitecore.FakeDb.DbItem("theItem", ID.NewID, ID.NewID);
var field = new Sitecore.FakeDb.DbLinkField("theField")
{
    Anchor = "anchor",
    Class = "class",
    LinkType = "linktype",
    QueryString = "querystring",
    Target = "target",
    TargetID = ID.NewID,
    Text = "text",
    Title = "title",
    Url = "url",
};

item.Add(field);

var db = new Sitecore.FakeDb.Db("web");
db.Add(item);

var fakeItem = db.GetItem("/sitecore/content/theItem");
LinkField theField = fakeItem.Fields["theField"];
1
  • Yes, I didn't post that part but it is added to the Db. The only problem is with this kind of field. Items, templates, inheritance... that is working good.
    – jgondev
    Commented Dec 15, 2016 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.