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?

  • Which version FakeDb/Sitecore do you use? – Tamás Tárnok Dec 15 '16 at 13:33
  • Sitecore 8.1 Initial Release, Sitecore.FakeDb 1.3.1 – jgondev Dec 15 '16 at 13:46
  • Don't you have the same issue when you are using real Sitecore items? – Tamás Tárnok Dec 15 '16 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 Dec 15 '16 at 14:05
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")
                }
            }

        }
    }
});
| improve this answer | |
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"];
}
| improve this answer | |
  • 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 Dec 15 '16 at 14:09
  • Hm, let's post the the more complex structure into your question. – Tamás Tárnok Dec 15 '16 at 14:15
  • I've edited the question with the exact structure – jgondev Dec 15 '16 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"];
| improve this answer | |
  • 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 Dec 15 '16 at 13:10

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.