How to get all the fields from Sitecore

693 views Asked by At

I'm working on Sitecore 6.6. I wan to get all the fields for a particular item.

I'm getting all the fields using this

FieldCollection fieldcollection = Sitecore.Context.Database.GetItem(Id, LanguageManager.GetLanguages(Sitecore.Context.Database).Where(i => i.CultureInfo.ToString() == Globallanguage).FirstOrDefault(), Sitecore.Data.Version.Latest).Fields;

And looping through each field and adding items to a list

    foreach (Field field in fieldcollection)
    {
        if(fields!=null)
        {
        .....
        }
    }

But after returning some fields it is throwing me an error

Object reference not set to an instance of an object. at Code.Pipeline.NotFoundRedirector.GetAllMappedUrls()

I wanted to know whether I'm using correct method to get all fields

1

There are 1 answers

0
Ajay A On

I have got all the fields by changing the version and adding getall() method

 FieldCollection fieldcollection = Sitecore.Context.Database.GetItem(TargetId, LanguageManager.GetLanguages(Sitecore.Context.Database).Where(i => i.CultureInfo.ToString() == Globallanguage).FirstOrDefault(), Sitecore.Context.Database.GetItem(TargetId).Version).Fields;        
 fieldcollection.ReadAll();

The loop goes as usual

   foreach (Field field in fieldcollection)
    {
        if(fields!=null)
        {
        .....
        }
    }