Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I've got a 2 SharePoint lists:

List A: This is an external list that's using Business Connectivity Services to connect to CRM. List B: This list contains a lookup field that linked into List A.

I am using the SharePoint web service: http://myserver/_vti_bin/Lists.asmx, to insert the data.

Problem: The value is inserted, however, it's not linked into BCS. (i.e. When I go to edit mode in the inserted list item, the field is blank).

Methods tried: I have tried using various fields in my List A, including the ID Field and other fields, but none works. I have also tried retrieving List A items using the web service, and use the value pattern (in this case: it's just the customer name in CRM), but it still doesnt work.

Possible cause of error: The value pattern of the BCS inserted is wrong.

Any ideas guys?

share|improve this question
add comment

2 Answers

so we had to do teh same thing a little bit ago.

the final call is

            var entInst = BcsItem.GetEntityInstance(web.Site, "BEIM.BCS", "BeimBusinessUnit", prepop.BusinessUnit.Id.ToString());
            SPBusinessDataField dataField = threeyearlyFileItem.Fields["Business Unit"] as SPBusinessDataField;

            threeyearlyFileItem[dataField.RelatedField] = prepop.BusinessUnit.Id.ToString();

            BcsItem.SetSecondaryFields(threeyearlyFileItem, dataField, entInst);  

You hvae to set teh field then set secondary fields

the class BcsItem does all teh hard work it is as follows

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.BusinessData.MetadataModel.Collections;
using Microsoft.BusinessData.Runtime;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.BusinessData.SharedService;

namespace BEIM.Webservices
{
    public class BcsItem
    {

        /// <summary>
        /// This hits the BCS and gets the instance basaed ont he key and the BCS details
        /// </summary>
        /// <returns></returns>
        public static IEntityInstance GetEntityInstance(SPSite site, string nameSpace, string entityName, string key)
        {
            BdcService service = SPFarm.Local.Services.GetValue<BdcService>(String.Empty);
            SPServiceContext serviceContext = SPServiceContext.GetContext(site);
            IMetadataCatalog catalog = service.GetDatabaseBackedMetadataCatalog(serviceContext);
            IEntity entity = catalog.GetEntity(nameSpace, entityName);
            ILobSystemInstance LobSysteminstance = entity.GetLobSystem().GetLobSystemInstances()[0].Value;

            IEntityInstance iEInstance = null;
            Identity id = new Identity(key);

            //entity.GetIdentifiers();

            iEInstance = entity.FindSpecific(id, LobSysteminstance);
            return iEInstance;

        }

        /// <summary>  

        /// Set all secondary BCS fields for a given entity  
        /// </summary>  

        /// <param name="listItem">The item to set the fields for</param>  
        /// <param name="dataField">The BCS field itself</param>  
        /// <param name="entityInstance">The entity to get the values from</param>  

        public static void SetSecondaryFields(SPListItem listItem, SPBusinessDataField dataField, IEntityInstance entityInstance)
        {


            // Convert the entity to a formatted datatable  

            DataTable dtBDCData = entityInstance.EntityAsFormattedDataTable;

            // Set the BCS field itself (Display Value)  

            listItem[dataField.Id] = dtBDCData.Rows[0][dataField.BdcFieldName].ToString();

            // Get the specific finder method to get the columns that returns  
            IMethodInstance method = entityInstance.Entity.GetMethodInstances(MethodInstanceType.SpecificFinder)[0].Value;

            ITypeDescriptorCollection oDescriptors = method.GetReturnTypeDescriptor().GetChildTypeDescriptors()[0].GetChildTypeDescriptors();

            // Set the column names to the correct values  

            foreach (ITypeDescriptor oType in oDescriptors)
            {

                if (oType.ContainsLocalizedDisplayName())
                {

                    if (dtBDCData.Columns.Contains(oType.Name))
                    {

                        dtBDCData.Columns[oType.Name].ColumnName = oType.GetLocalizedDisplayName();

                    }

                }

            }

            // get the secondary field display names; these should be set  

            string[] sSecondaryFieldsDisplayNames = dataField.GetSecondaryFieldsNames();

            // loop through the fields and set each column to its value  

            foreach (string columnNameint in sSecondaryFieldsDisplayNames)
            {

                Guid gFieldID = listItem.Fields[String.Format("{0}: {1}", dataField.Title, columnNameint)].Id;

                listItem[gFieldID] = dtBDCData.Rows[0][columnNameint].ToString();

            }

        }

    }
}
share|improve this answer
add comment

I have some sort of same kind of problem. I have a List with lookup field which can hold up multiples. on the code I'm correctly populating the above said field, but it is not get reflected on the List. Any suggestions ?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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