public Dictionary <string, List <FieldContract> > GetItemsFields(List <string> oItemsIds, string sDatabaseName) { #region VARIABLES Dictionary <string, List <FieldContract> > oItemsWithFieldsToReturn; List <FieldContract> oItemFieldsToReturn; FieldContract oFieldSerializable; Sitecore.Data.ID oItemId; Database oDatabase; FieldCollection oItemFields; Sitecore.Data.Version[] oItemVersions; LanguageCollection oLanguages; int iNumberOfFields; Field oCurrentField; Item oItem; //Add variables here... #endregion Sitecore.Diagnostics.Log.Info(string.Format("GetItemFields Begin {0}", oItemsIds), this); //List that will contain the fields to return oItemsWithFieldsToReturn = new Dictionary <string, List <FieldContract> >(); if (oItemsIds != null && !string.IsNullOrEmpty(sDatabaseName)) { oDatabase = Sitecore.Data.Database.GetDatabase(sDatabaseName); if (oDatabase != null) { foreach (String sItemId in oItemsIds) { Sitecore.Diagnostics.Log.Info(string.Format("Getting fields for item {0}", sItemId), this); if (ID.TryParse(sItemId, out oItemId) && !oItemsWithFieldsToReturn.ContainsKey(sItemId)) { oItemFieldsToReturn = new List <FieldContract>(); oLanguages = oDatabase.GetLanguages(); oItem = oDatabase.GetItem(oItemId); oItemVersions = oItem.Versions.GetVersionNumbers(); if (oItemVersions != null && oLanguages != null) { foreach (var oVersion in oItemVersions) { foreach (var oItemLanguage in oLanguages) { oItem = oDatabase.GetItem(oItemId, oItemLanguage, oVersion); if (oItem != null) { //We want to assure that no lazy loading is used oItem.Fields.ReadAll(); oItemFields = oItem.Fields; //Getting everything ready for the serialization if (oItemFields != null) { iNumberOfFields = oItemFields.Count; for (int iFieldIndex = 0; iFieldIndex < iNumberOfFields; iFieldIndex++) { oCurrentField = oItemFields[iFieldIndex]; oFieldSerializable = new FieldContract() { Id = oCurrentField.ID.ToString(), Value = oCurrentField.Value, Language = oItemLanguage.Name, Version = oVersion.Number }; oItemFieldsToReturn.Add(oFieldSerializable); Sitecore.Diagnostics.Log.Debug(string.Format("Field {0} Value {1}", oFieldSerializable.Id, oFieldSerializable.Value), this); } } } else { Sitecore.Diagnostics.Log.Error(string.Format("Unable to get Item for item ID:{0}", oItemsIds), this); } } } } oItemsWithFieldsToReturn.Add(sItemId, oItemFieldsToReturn); } } } } Sitecore.Diagnostics.Log.Info(string.Format("GetItemFields End, FieldsCount:{0}", oItemsWithFieldsToReturn.Count), this); return(oItemsWithFieldsToReturn); }
/// <summary> /// Gets the fields associated to an item /// </summary> /// <param name="sItemId">Sitecore item ID</param> /// <param name="sLanguageName">Sitecore item Language</param> /// <param name="iItemVersionNumber">Sitecore item Version number</param> /// <param name="sDatabaseName">Sitecore Database to use</param> /// <param name="iProviderCount">Sitecore provider count</param> /// <returns>A List witht the fields associated to the item with the given id</returns> public List <FieldContract> GetItemFields(string sItemId, string sLanguageName, int iItemVersionNumber, string sDatabaseName) { #region VARIABLES List <FieldContract> oFieldListToReturn; FieldContract oFieldSerializable; Sitecore.Data.ID oItemId; DataProvider oWebDataProvider; CallContext oWebCallContext; FieldCollection oItemFields; Sitecore.Data.VersionUri oItemVersionUri; Sitecore.Globalization.Language oItemLanguage; Sitecore.Data.Version oItemVersion; int iNumberOfFields; Field oCurrentField; Item oItem; //Add variables here... #endregion Sitecore.Diagnostics.Log.Info(string.Format("GetItemFields Begin {0}", sItemId), this); //List that will contain the fields to return oFieldListToReturn = new List <FieldContract>(); if (!string.IsNullOrEmpty(sItemId) && ID.IsID(sItemId) && !string.IsNullOrEmpty(sDatabaseName) && !string.IsNullOrEmpty(sLanguageName)) { //Building CallContext and VersionUri to initialize the data provider oItemId = new Sitecore.Data.ID(sItemId); if (string.IsNullOrEmpty(sDatabaseName)) { sDatabaseName = this.DefaultDatabase; } if (string.IsNullOrEmpty(sLanguageName)) { sLanguageName = DefaultLanguage; } if (iItemVersionNumber == -1) { oItemVersion = DefaultVersion; } else { oItemVersion = new Sitecore.Data.Version(iItemVersionNumber); } oItemLanguage = Sitecore.Globalization.Language.Parse(sLanguageName); oItemLanguage.Origin.ItemId = oItemId; if (oItemLanguage != null) { oItemVersionUri = new VersionUri(oItemLanguage, oItemVersion); oWebCallContext = BuildCallContext(sDatabaseName);; oWebDataProvider = GetDataProvider(sDatabaseName); if (oWebDataProvider != null) { //To get the item fields we first need to get the item definition oItemFields = null; oItem = oWebDataProvider.Database.GetItem(oItemId, oItemLanguage, oItemVersion); if (oItem != null) { //We want to assure that no lazy loading is used oItem.Fields.ReadAll(); oItemFields = oItem.Fields; //Serialization if (oItemFields != null) { iNumberOfFields = oItemFields.Count; for (int iFieldIndex = 0; iFieldIndex < iNumberOfFields; iFieldIndex++) { oCurrentField = oItemFields[iFieldIndex]; oFieldSerializable = new FieldContract() { Id = oCurrentField.ID.ToString(), Value = oCurrentField.Value }; oFieldListToReturn.Add(oFieldSerializable); Sitecore.Diagnostics.Log.Error(string.Format("Field {0} Value {1}", oFieldSerializable.Id, oFieldSerializable.Value), this); } } } else { Sitecore.Diagnostics.Log.Error(string.Format("Unable to get Item for item ID:{0}", sItemId), this); } } else { Sitecore.Diagnostics.Log.Error("Unable to find data provide", this); } } else { Sitecore.Diagnostics.Log.Error(string.Format("Unable to find language {0}", sLanguageName), this); } } Sitecore.Diagnostics.Log.Info(string.Format("GetItemFields End, FieldsCount:{0}", oFieldListToReturn.Count), this); return(oFieldListToReturn); }