<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3483401514643638890</id><updated>2012-02-16T05:18:14.141-08:00</updated><category term='C#'/><category term='Vista'/><category term='QueryString'/><category term='Intro'/><category term='Programmatically'/><category term='EntityInstanceIDEncoder'/><category term='Microsoft Office 2003'/><category term='BDC'/><category term='Integrated Security'/><category term='Business Data Catalog'/><category term='SharePoint'/><category term='MOSS'/><category term='Redirection'/><category term='JavaScript'/><category term='Redirect'/><category term='WSS'/><category term='Credentials'/><title type='text'>Wade's SharePoint Solutions</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://wadehuntersblog.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3483401514643638890/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://wadehuntersblog.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Wade Hunter - B.Sc Computer Science, MCP, MCTS</name><uri>http://www.blogger.com/profile/10725503127402427147</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3483401514643638890.post-6305729350860742867</id><published>2010-10-23T06:26:00.000-07:00</published><updated>2010-12-03T06:27:05.973-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SharePoint'/><category scheme='http://www.blogger.com/atom/ns#' term='Programmatically'/><category scheme='http://www.blogger.com/atom/ns#' term='BDC'/><title type='text'>Working with the SharePoint 2007 Business Data Catalog Programmatically</title><content type='html'>--EDIT ----------------------------------------------------------------------------------&lt;br /&gt;The methods below are compatable with MOSS 2007. The code has been reworked by Jasper Siegmund to work with SharePoint 2010. His blog and code can be viewed here: &lt;a href="http://jsiegmund.wordpress.com/2010/12/03/sp2010-setting-bcs-column-and-related-fields/"&gt;http://jsiegmund.wordpress.com/2010/12/03/sp2010-setting-bcs-column-and-related-fields/&lt;/a&gt;&lt;br /&gt;-------------------------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;A few years back I created a post on how to programmatically update the value of a Business Data Column in SharePoint 2007. I wanted to provide a follow up to that article as I have since developed more methods of working with business data. The following code is a class that I developed to work with business data and business data columns. It's not very well commented but hopefully it is clear enough for someone to take the bits and pieces they need to develop their own solutions.&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Globalization;&lt;br /&gt;using System.Xml;&lt;br /&gt;using Microsoft.Office.Server;&lt;br /&gt;using Microsoft.Office.Server.ApplicationRegistry.Infrastructure;&lt;br /&gt;using Microsoft.Office.Server.ApplicationRegistry.MetadataModel;&lt;br /&gt;using Microsoft.Office.Server.ApplicationRegistry.Runtime;&lt;br /&gt;using Microsoft.SharePoint;&lt;br /&gt;using Microsoft.SharePoint.Portal.WebControls;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class SPBusinessData&lt;br /&gt;{&lt;br /&gt;#region ********** Public Static Methods **********&lt;br /&gt;&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Searches the specified BDC entity and returns the results in a DataTable&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;public static DataTable GetItems(SPSite oSite, string sInstanceName, string sEntityName, string sSearchString)&lt;br /&gt;{&lt;br /&gt;SetServerContext(oSite);&lt;br /&gt;&lt;br /&gt;LobSystemInstance oLobInstance = GetLobSystemInstance(sInstanceName);&lt;br /&gt;Entity oEntity = GetEntity(oLobInstance, sEntityName);&lt;br /&gt;IEntityInstance oEntityInstance = null;&lt;br /&gt;&lt;br /&gt;//Search the entity&lt;br /&gt;FilterCollection fc = oEntity.GetFinderFilters();&lt;br /&gt;if (fc[0] is WildcardFilter)&lt;br /&gt;{&lt;br /&gt;((WildcardFilter)fc[0]).Value = "%" + sSearchString + "%";&lt;br /&gt;}&lt;br /&gt;else if (fc[0] is ComparisonFilter)&lt;br /&gt;{&lt;br /&gt;((ComparisonFilter)fc[0]).Value = sSearchString;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;IEntityInstanceEnumerator prodEntityInstanceEnumerator = oEntity.FindFiltered(fc, oLobInstance);&lt;br /&gt;string[] sFields = GetFields(oSite, sInstanceName, sEntityName);&lt;br /&gt;&lt;br /&gt;DataTable oData = new DataTable();&lt;br /&gt;oData.TableName = oEntity.Name;&lt;br /&gt;&lt;br /&gt;foreach (string sField in sFields)&lt;br /&gt;{&lt;br /&gt;oData.Columns.Add(sField);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;while (prodEntityInstanceEnumerator.MoveNext())&lt;br /&gt;{&lt;br /&gt;oEntityInstance = prodEntityInstanceEnumerator.Current;&lt;br /&gt;DataTable dtData = oEntityInstance.EntityAsFormattedDataTable;&lt;br /&gt;oData.Rows.Add(dtData.Rows[0].ItemArray);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;string sIDColumn = GetIdentifierName(oLobInstance, oEntity);&lt;br /&gt;oData.Columns[sIDColumn].SetOrdinal(0);&lt;br /&gt;&lt;br /&gt;return oData;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Retrieves the item(s) with the specified ID&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;public static DataTable GetItemsFromID(SPWeb oWeb, string sID, string sInstanceName, string sEntityName)&lt;br /&gt;{&lt;br /&gt;SetServerContext(oWeb.Site);&lt;br /&gt;LobSystemInstance oLobInstance = GetLobSystemInstance(sInstanceName);&lt;br /&gt;Entity oEntity = GetEntity(oLobInstance, sEntityName);&lt;br /&gt;&lt;br /&gt;IEntityInstance oEntityInstance = null;&lt;br /&gt;DataTable dtBDCData = null;&lt;br /&gt;&lt;br /&gt;if (EntityInstanceIdEncoder.IsEncodedIdentifier(sID))&lt;br /&gt;{&lt;br /&gt;oEntityInstance = GetBDCItemFromEncodedID(sID, oEntity, oLobInstance);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;object oID = GetTypedIDValue(sID, oEntity);&lt;br /&gt;string sEncodedIdentifier = EntityInstanceIdEncoder.EncodeEntityInstanceId(new object[] { oID });&lt;br /&gt;if (EntityInstanceIdEncoder.IsEncodedIdentifier(sEncodedIdentifier))&lt;br /&gt;{&lt;br /&gt;oEntityInstance = oEntity.FindSpecific(new object[] { oID }, oLobInstance);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;if (oEntityInstance != null)&lt;br /&gt;{&lt;br /&gt;dtBDCData = GetBDCData(oEntityInstance);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;return dtBDCData;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Retrieves the list of fields that the specified BDC entity is configured to return&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;public static string[] GetFields(SPSite oSite, string sInstanceName, string sEntityName)&lt;br /&gt;{&lt;br /&gt;string[] sFields;&lt;br /&gt;&lt;br /&gt;SetServerContext(oSite);&lt;br /&gt;&lt;br /&gt;LobSystemInstance oLobInstance = GetLobSystemInstance(sInstanceName);&lt;br /&gt;Entity oEntity = GetEntity(oLobInstance, sEntityName);&lt;br /&gt;TypeDescriptorCollection oDescriptors = oEntity.GetSpecificFinderMethodInstance().GetReturnTypeDescriptor().GetChildTypeDescriptors()[0].GetChildTypeDescriptors();&lt;br /&gt;&lt;br /&gt;List&lt;string&gt; oFields = new List&lt;string&gt;();&lt;br /&gt;foreach (TypeDescriptor oType in oDescriptors)&lt;br /&gt;{&lt;br /&gt;if (oType.ContainsLocalizedDisplayName())&lt;br /&gt;{&lt;br /&gt;oFields.Add(oType.GetLocalizedDisplayName());&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;oFields.Add(oType.Name);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sFields = oFields.ToArray();&lt;br /&gt;&lt;br /&gt;return sFields;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Sets a BDC field on a list item, given the item's encoded or unencoded ID&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;public static void SetFieldByID(SPListItem oListItem, String sColumnName, string sID)&lt;br /&gt;{&lt;br /&gt;BusinessDataField oBDCField = (BusinessDataField)oListItem.Fields.GetFieldByInternalName(sColumnName);&lt;br /&gt;String sEntityName = oBDCField.EntityName;&lt;br /&gt;String sInstanceName = oBDCField.SystemInstanceName;&lt;br /&gt;&lt;br /&gt;SetServerContext(oListItem.ParentList.ParentWeb.Site);&lt;br /&gt;&lt;br /&gt;LobSystemInstance oLobInstance = GetLobSystemInstance(sInstanceName);&lt;br /&gt;Entity oEntity = GetEntity(oLobInstance, sEntityName);&lt;br /&gt;IEntityInstance oEntityInstance = null;&lt;br /&gt;&lt;br /&gt;if (EntityInstanceIdEncoder.IsEncodedIdentifier(sID))&lt;br /&gt;{&lt;br /&gt;//the key is already encoded&lt;br /&gt;object[] oIDList = EntityInstanceIdEncoder.DecodeEntityInstanceId(sID);&lt;br /&gt;oEntityInstance = oEntity.FindSpecific(oIDList[0], oLobInstance);&lt;br /&gt;oListItem[oBDCField.RelatedField] = sID.ToString();&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;object oID = GetTypedIDValue(sID, oEntity);&lt;br /&gt;string sEncodedIdentifier = EntityInstanceIdEncoder.EncodeEntityInstanceId(new object[] { oID });&lt;br /&gt;if (EntityInstanceIdEncoder.IsEncodedIdentifier(sEncodedIdentifier))&lt;br /&gt;{&lt;br /&gt;oEntityInstance = oEntity.FindSpecific(new object[] { oID }, oLobInstance);&lt;br /&gt;oListItem[oBDCField.RelatedField] = sEncodedIdentifier;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;if (oEntityInstance != null)&lt;br /&gt;{&lt;br /&gt;SetSecondaryFields(oListItem, oBDCField, oEntityInstance);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Returns the name of the identifier field for the given entity&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;public static string GetIdentifierName(SPSite oSite, string sInstanceName, string sEntityName)&lt;br /&gt;{&lt;br /&gt;SetServerContext(oSite);&lt;br /&gt;LobSystemInstance oLobInstance = GetLobSystemInstance(sInstanceName);&lt;br /&gt;Entity oEntity = GetEntity(oLobInstance, sEntityName);&lt;br /&gt;return GetIdentifierName(oLobInstance, oEntity);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#endregion&lt;br /&gt;&lt;br /&gt;#region ********** Private Static Methods **********&lt;br /&gt;&lt;br /&gt;private static string GetIdentifierName(LobSystemInstance oLobInstance, Entity oEntity)&lt;br /&gt;{&lt;br /&gt;return oEntity.GetIdentifiers()[0].Name.Replace("[", string.Empty).Replace("]", string.Empty);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private static void SetServerContext(SPSite oSite)&lt;br /&gt;{&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;ServerContext oContext = ServerContext.GetContext(oSite);&lt;br /&gt;SqlSessionProvider.Instance().SetSharedResourceProviderToUse(oContext);&lt;br /&gt;}&lt;br /&gt;catch { }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private static Entity GetEntity(LobSystemInstance oLobInstance, string sEntityName)&lt;br /&gt;{&lt;br /&gt;Entity oEntity = oLobInstance.GetEntities()[sEntityName];&lt;br /&gt;return oEntity;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private static LobSystemInstance GetLobSystemInstance(string sInstanceName)&lt;br /&gt;{&lt;br /&gt;NamedLobSystemInstanceDictionary oLobInstanceDic = ApplicationRegistry.GetLobSystemInstances();&lt;br /&gt;LobSystemInstance oLobInstance = oLobInstanceDic[sInstanceName];;&lt;br /&gt;&lt;br /&gt;return oLobInstance;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private static IEntityInstance GetBDCItemFromEncodedID(string sEncodedID, Entity oEntity, LobSystemInstance oLobInstance)&lt;br /&gt;{&lt;br /&gt;object[] oIDList = EntityInstanceIdEncoder.DecodeEntityInstanceId(sEncodedID);&lt;br /&gt;return oEntity.FindSpecific(oIDList[0], oLobInstance);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private static DataTable GetBDCData(IEntityInstance oEntityInstance)&lt;br /&gt;{&lt;br /&gt;DataTable dtBDCData = oEntityInstance.EntityAsFormattedDataTable;&lt;br /&gt;dtBDCData.TableName = oEntityInstance.Entity.Name;&lt;br /&gt;&lt;br /&gt;TypeDescriptorCollection oDescriptors = oEntityInstance.Entity.GetSpecificFinderMethodInstance().GetReturnTypeDescriptor().GetChildTypeDescriptors()[0].GetChildTypeDescriptors();&lt;br /&gt;foreach (TypeDescriptor oType in oDescriptors)&lt;br /&gt;{&lt;br /&gt;if (oType.ContainsLocalizedDisplayName())&lt;br /&gt;{&lt;br /&gt;if (dtBDCData.Columns.Contains(oType.Name))&lt;br /&gt;{&lt;br /&gt;dtBDCData.Columns[oType.Name].ColumnName = oType.GetLocalizedDisplayName();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;//Set the ID column to ordinal 0&lt;br /&gt;string sIDColumn = oEntityInstance.Entity.GetIdentifiers()[0].Name.Replace("[", string.Empty).Replace("]", string.Empty);&lt;br /&gt;dtBDCData.Columns[sIDColumn].SetOrdinal(0);&lt;br /&gt;&lt;br /&gt;return dtBDCData;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private static IEntityInstance GetEntityInstanceFromID(string sID, Entity oEntity, LobSystemInstance oLobInstance)&lt;br /&gt;{&lt;br /&gt;IEntityInstance oEntityInstance = null;&lt;br /&gt;IdentifierCollection oIDCollection = oEntity.GetIdentifiers();&lt;br /&gt;String sIdentifierType = oIDCollection[0].IdentifierType.Name.ToLower().Replace("system.", String.Empty);&lt;br /&gt;object oID = GetTypedIDValue(sID, oEntity);&lt;br /&gt;&lt;br /&gt;if (oID != null)&lt;br /&gt;{&lt;br /&gt;oEntityInstance = oEntity.FindSpecific(oID, oLobInstance);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;return oEntityInstance;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private static object GetTypedIDValue(string sID, Entity oEntity)&lt;br /&gt;{&lt;br /&gt;IdentifierCollection oIDCollection = oEntity.GetIdentifiers();&lt;br /&gt;String sIdentifierType = oIDCollection[0].IdentifierType.Name.ToLower().Replace("system.", String.Empty);&lt;br /&gt;object oID = null;&lt;br /&gt;&lt;br /&gt;//find the instance value based on the given identifier type&lt;br /&gt;switch (sIdentifierType)&lt;br /&gt;{&lt;br /&gt;case "string":&lt;br /&gt;oID = sID;&lt;br /&gt;break;&lt;br /&gt;case "datetime":&lt;br /&gt;oID = DateTime.Parse(sID, CultureInfo.CurrentCulture);&lt;br /&gt;break;&lt;br /&gt;case "boolean":&lt;br /&gt;oID = Boolean.Parse(sID);&lt;br /&gt;break;&lt;br /&gt;case "int32":&lt;br /&gt;oID = Int32.Parse(sID);&lt;br /&gt;break;&lt;br /&gt;case "int16":&lt;br /&gt;oID = Int16.Parse(sID);&lt;br /&gt;break;&lt;br /&gt;case "double":&lt;br /&gt;oID = Double.Parse(sID);&lt;br /&gt;break;&lt;br /&gt;case "char":&lt;br /&gt;oID = Char.Parse(sID);&lt;br /&gt;break;&lt;br /&gt;case "guid":&lt;br /&gt;oID = new Guid(sID);&lt;br /&gt;break;&lt;br /&gt;default:&lt;br /&gt;oID = sID;&lt;br /&gt;break;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;return oID;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private static void SetSecondaryFields(SPListItem oListItem, BusinessDataField oBDCField, IEntityInstance oEntityInstance)&lt;br /&gt;{&lt;br /&gt;DataTable dtBDCData = oEntityInstance.EntityAsFormattedDataTable;&lt;br /&gt;&lt;br /&gt;//Set display field&lt;br /&gt;oListItem[oBDCField.Id] = dtBDCData.Rows[0][oBDCField.BdcFieldName].ToString();&lt;br /&gt;&lt;br /&gt;//Setup localized display names&lt;br /&gt;TypeDescriptorCollection oDescriptors = oEntityInstance.Entity.GetSpecificFinderMethodInstance().GetReturnTypeDescriptor().GetChildTypeDescriptors()[0].GetChildTypeDescriptors();&lt;br /&gt;foreach (TypeDescriptor oType in oDescriptors)&lt;br /&gt;{&lt;br /&gt;if (oType.ContainsLocalizedDisplayName())&lt;br /&gt;{&lt;br /&gt;if (dtBDCData.Columns.Contains(oType.Name))&lt;br /&gt;{&lt;br /&gt;dtBDCData.Columns[oType.Name].ColumnName = oType.GetLocalizedDisplayName();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;string[] sSecondaryFieldsDisplayNames = oBDCField.GetSecondaryFieldsNames();&lt;br /&gt;string[] sSecondaryFieldsInternalNames = GetSecondaryInternalFieldNames(oBDCField);&lt;br /&gt;&lt;br /&gt;for (int i = 0; i &amp;lt; sSecondaryFieldsDisplayNames.Length; i++)&lt;br /&gt;{&lt;br /&gt;Guid gFieldID = oListItem.Fields.GetFieldByInternalName(sSecondaryFieldsInternalNames[i]).Id;&lt;br /&gt;oListItem[gFieldID] = dtBDCData.Rows[0][sSecondaryFieldsDisplayNames[i]].ToString();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private static string[] GetSecondaryInternalFieldNames(BusinessDataField oBDCField)&lt;br /&gt;{&lt;br /&gt;XmlDocument xmlData = new XmlDocument();&lt;br /&gt;xmlData.LoadXml(oBDCField.SchemaXml);&lt;br /&gt;String sFieldsString = xmlData.FirstChild.Attributes["SecondaryFieldsWssStaticNames"].Value;&lt;br /&gt;&lt;br /&gt;return sFieldsString.Split(':');&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#endregion&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3483401514643638890-6305729350860742867?l=wadehuntersblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://wadehuntersblog.blogspot.com/feeds/6305729350860742867/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3483401514643638890&amp;postID=6305729350860742867' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3483401514643638890/posts/default/6305729350860742867'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3483401514643638890/posts/default/6305729350860742867'/><link rel='alternate' type='text/html' href='http://wadehuntersblog.blogspot.com/2010/10/working-with-sharepoint-2007-business.html' title='Working with the SharePoint 2007 Business Data Catalog Programmatically'/><author><name>Wade Hunter - B.Sc Computer Science, MCP, MCTS</name><uri>http://www.blogger.com/profile/10725503127402427147</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3483401514643638890.post-224250266950163141</id><published>2008-10-21T14:20:00.001-07:00</published><updated>2010-10-23T06:46:10.573-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Business Data Catalog'/><category scheme='http://www.blogger.com/atom/ns#' term='SharePoint'/><category scheme='http://www.blogger.com/atom/ns#' term='WSS'/><category scheme='http://www.blogger.com/atom/ns#' term='Programmatically'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='BDC'/><category scheme='http://www.blogger.com/atom/ns#' term='EntityInstanceIDEncoder'/><category scheme='http://www.blogger.com/atom/ns#' term='MOSS'/><title type='text'>Programmatically set a BDC column</title><content type='html'>********** UPDATE **********&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;I've created a &lt;/span&gt;&lt;a style="font-style: italic;" href="http://wadehuntersblog.blogspot.com/2010/10/working-with-sharepoint-2007-business.html"&gt;new post&lt;/a&gt;&lt;span style="font-style: italic;"&gt; as a follow up to this article. It contains C# code for a class I created to work with business data in SharePoint 2007. There is a public method in there called "SetFieldByID" which will set the value of a BDC column including all auxiliary fields. &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Click here to &lt;/span&gt;&lt;a style="font-style: italic;" href="http://wadehuntersblog.blogspot.com/2010/10/working-with-sharepoint-2007-business.html"&gt;view &lt;/a&gt;&lt;span style="font-style: italic;"&gt;the post.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;*****************************&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Finally!&lt;br /&gt;&lt;br /&gt;I spent way too much time playing around with this one... There is no documentation, and very little discussion about it in the forums. All I wanted to do was be able to set the value(s) of a Business Data Catalog field programatically through the SharePoint object model.&lt;br /&gt;&lt;br /&gt;Sounds simple enough right?&lt;br /&gt;&lt;br /&gt;Not quite....&lt;br /&gt;&lt;br /&gt;The first thing to note is that you can't just set the value of the column like you would do with other fields like so:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;mySPListItem["myBDCField"] = 123;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If you do this, you won't get an error, but the value will not be set...&lt;br /&gt;&lt;br /&gt;This reference was a useful start: &lt;a href="http://www.portalsolutions.net/Blog/Lists/Posts/Post.aspx?ID=18"&gt;http://www.portalsolutions.net/Blog/Lists/Posts/Post.aspx?ID=18&lt;/a&gt;. However, I still encountered some challenges in setting a BDC column value.&lt;br /&gt;&lt;br /&gt;First of all, the &lt;span style="color: rgb(204, 102, 0);"&gt;EntityInstanceIdEncoder&lt;/span&gt; class is found in the following namespace:&lt;br /&gt;&lt;span style="color: rgb(255, 204, 51);"&gt;&lt;span style="color: rgb(204, 102, 0);"&gt;Microsoft.Office.Server.ApplicationRegistry.Infrastructure;&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;The referenced dll is found in the ISAPI folder on the SharePoint server:&lt;br /&gt;&lt;span style="color: rgb(204, 102, 0);"&gt;C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Now for some code...&lt;br /&gt;&lt;br /&gt;To check if a field is a BDC field:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;if(spItem.Fields["SomeField"].TypeAsString == "BusinessData")&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;/span&gt;&lt;br /&gt;To get the BDC field:&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;SPField bdcField = spItem.Fields[sBDCColumnName];&lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;To get the BDC column entity internal name (this is key):&lt;/span&gt;&lt;br /&gt;XmlDocument xmlData = new XmlDocument();&lt;br /&gt;xmlData.LoadXml(bdcField.SchemaXml);&lt;br /&gt;String sEntityName = xmlData.FirstChild.Attributes["RelatedFieldWssStaticName"].Value;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Set the primary key of the BDC field:&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;spItem[sEntityName] = EntityInstanceIdEncoder.EncodeEntityInstanceId(new object[] { Value }); &lt;/span&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;&lt;em&gt;- Here you may want to add some logic to cast the the passed value to the proper type.&lt;/em&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Set the display value of the column:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;spItem[sBDCColumnName] = Value;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;You also need to explicitly set the values of any of the additional related fields. These values can be set like any other column:&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;spItem[&lt;bdccolumnname&gt; BDCFieldName + ": " + BDCDisplayFieldName &lt;relatedfield&gt;] = "SomeValue"; &lt;/relatedfield&gt;&lt;/bdccolumnname&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;// ex: spItem["Products: Name"] = "XYZ";&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(0, 0, 0);"&gt;Hope this saves somebody the trouble that I went through to figure this out!&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3483401514643638890-224250266950163141?l=wadehuntersblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://wadehuntersblog.blogspot.com/feeds/224250266950163141/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3483401514643638890&amp;postID=224250266950163141' title='19 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3483401514643638890/posts/default/224250266950163141'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3483401514643638890/posts/default/224250266950163141'/><link rel='alternate' type='text/html' href='http://wadehuntersblog.blogspot.com/2008/10/progrmatically-set-bdc-column.html' title='Programmatically set a BDC column'/><author><name>Wade Hunter - B.Sc Computer Science, MCP, MCTS</name><uri>http://www.blogger.com/profile/10725503127402427147</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>19</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3483401514643638890.post-6332259689698873367</id><published>2008-07-24T08:23:00.000-07:00</published><updated>2008-10-28T07:59:39.186-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Integrated Security'/><category scheme='http://www.blogger.com/atom/ns#' term='SharePoint'/><category scheme='http://www.blogger.com/atom/ns#' term='WSS'/><category scheme='http://www.blogger.com/atom/ns#' term='Vista'/><category scheme='http://www.blogger.com/atom/ns#' term='Microsoft Office 2003'/><category scheme='http://www.blogger.com/atom/ns#' term='MOSS'/><category scheme='http://www.blogger.com/atom/ns#' term='Credentials'/><title type='text'>Prevent Microsoft Office from prompting users for credentials</title><content type='html'>If you're running Windows Vista and you try to open a Microsoft Office 2007 document from a &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_0"&gt;SharePoint&lt;/span&gt; 2007 site, you may be prompted for your user credentials &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_1"&gt;every time&lt;/span&gt;. This can be extremely annoying and frustrating for clients who use &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_2"&gt;SharePoint&lt;/span&gt; as there primary document management system. You'd think you could get around this problem simply by adding the &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_3"&gt;sharepoint&lt;/span&gt; &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_4"&gt;url&lt;/span&gt; to your list of Trusted Sites, or Intranet Sites in IE, but for many, this is not the case. If your &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_5"&gt;SharePoint&lt;/span&gt; &lt;span class="blsp-spelling-error" id="SPELLING_ERROR_6"&gt;url&lt;/span&gt; contains a period (ex: &lt;a href="http://sharepoint.myorg/"&gt;http://sharepoint.myorg/&lt;/a&gt;, &lt;a href="https://sharepoint.myorg.com/"&gt;https://sharepoint.myorg.com/&lt;/a&gt;, etc), Vista takes the liberty of assuming that it is an &lt;span class="blsp-spelling-corrected" id="SPELLING_ERROR_7"&gt;Internet&lt;/span&gt; site, and not part of the local intranet.&lt;br /&gt;&lt;br /&gt;And you know what happens when you assume.... Well, normally you'd make an ass out of you and me, but in Vista's case, you just piss off a lot of people.&lt;br /&gt;&lt;br /&gt;Luckily, Microsoft has acknowledged this as being a bug, and has released a fix. Unfortunately, this is a client side fix, so it requires updating all client machines. A pain yes, but as of now, it is the only solution I have found to actually work.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Read the full KB article here:&lt;br /&gt;&lt;a href="http://support.microsoft.com/?id=943280"&gt;http://support.microsoft.com/?id=943280&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;If you have SP1 installed, all that is required is to add a key into the registry at the following location:&lt;br /&gt;&lt;br /&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_8"&gt;HKEY&lt;/span&gt;_LOCAL_MACHINE\SYSTEM\&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_9"&gt;CurrentControlSet&lt;/span&gt;\Services\&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_10"&gt;WebClient&lt;/span&gt;\Parameters&lt;br /&gt;&lt;br /&gt;Add a new Multi-String Value key.&lt;br /&gt;Add your site as the key's value.&lt;br /&gt;Reset the Web Client service&lt;br /&gt;&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_11"&gt;Et&lt;/span&gt; Voila! (That's French for '&lt;span class="blsp-spelling-error" id="SPELLING_ERROR_12"&gt;Tadaa&lt;/span&gt;' - See, your learning so much!)&lt;br /&gt;&lt;br /&gt;If this does not work, make sure your site is added to the list of Intranet sites in IE.&lt;br /&gt;&lt;br /&gt;If you do not have SP1 installed then - well you should probably install SP1.... But if that's just not an option, Microsoft has released a Hot Fix for just this problem. It can be found at the url above as part of the KB article.&lt;br /&gt;&lt;br /&gt;Pretty straight forward. It's unfortunate that we're forced to implement such a workaround, but that's life. Life is all about repairing Microsoft's mistakes.&lt;br /&gt;&lt;br /&gt;I kid...&lt;br /&gt;&lt;br /&gt;Or do I?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3483401514643638890-6332259689698873367?l=wadehuntersblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://wadehuntersblog.blogspot.com/feeds/6332259689698873367/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3483401514643638890&amp;postID=6332259689698873367' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3483401514643638890/posts/default/6332259689698873367'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3483401514643638890/posts/default/6332259689698873367'/><link rel='alternate' type='text/html' href='http://wadehuntersblog.blogspot.com/2008/07/prevent-microsoft-office-from-prompting.html' title='Prevent Microsoft Office from prompting users for credentials'/><author><name>Wade Hunter - B.Sc Computer Science, MCP, MCTS</name><uri>http://www.blogger.com/profile/10725503127402427147</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3483401514643638890.post-4142215306657290139</id><published>2008-07-23T08:14:00.001-07:00</published><updated>2008-11-05T08:16:32.081-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JavaScript'/><category scheme='http://www.blogger.com/atom/ns#' term='Redirection'/><category scheme='http://www.blogger.com/atom/ns#' term='Redirect'/><category scheme='http://www.blogger.com/atom/ns#' term='SharePoint'/><category scheme='http://www.blogger.com/atom/ns#' term='WSS'/><category scheme='http://www.blogger.com/atom/ns#' term='QueryString'/><category scheme='http://www.blogger.com/atom/ns#' term='MOSS'/><title type='text'>Controlling the SharePoint Redirection URL</title><content type='html'>For anybody who has ever tried to customize SharePoint, you may have encountered the following problem:&lt;br /&gt;&lt;br /&gt;You create a page which has a link to a list item's EditForm.aspx, NewForm.aspx, or DispForm.aspx. However, if a user clicks one of these links, then subsequently closes that page by pushing Cancel or OK, they are redirected back to the corresponding list or library, rather than to the page they were previously on (i.e. your custom page).&lt;br /&gt;&lt;br /&gt;SharePoint uses a querystring argument to control the return url on any given page. By default, this parameter is set to the respective list or library for the current form.&lt;br /&gt;&lt;br /&gt;You can control the return url by modifying or adding this parameter to any link.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#000099;"&gt;&amp;lt;a href="/Lists/MyList/NewForm.aspx?Source=/Pages/MyCustomPage.aspx"&amp;gt;Click Me!&amp;lt;/a&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;When the link is clicked the user will be directed to the new form for "MyList". After clicking OK or Cancel, they will be redirected back to "MyCustomPage.aspx".&lt;br /&gt;&lt;br /&gt;Simple enough, right? Fine, let's dig a little deeper.&lt;br /&gt;&lt;br /&gt;This works great if you are creating your own link through a Content Editor WebPart or SharePoint designer, but what about if you are using one of the List web parts, and want to control the redirection for the 'Add new item' link, or some other content you can't modify directly.&lt;br /&gt;&lt;br /&gt;This is where we get to hack out a solution in JavaScript.&lt;br /&gt;&lt;br /&gt;To put it simply, you can create a custom JavaScript function to append the "Source" querystring argument to the desired links.&lt;br /&gt;&lt;br /&gt;For example, put the following into a text file, and save it as AddSourceUrlToLinks.js:&lt;br /&gt;&lt;br /&gt;**************************************************************************&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#000099;"&gt;//Tell SharePoint to run our function when the page loads&lt;br /&gt;_spBodyOnLoadFunctionNames.push("appendSourceUrlToLinks");&lt;br /&gt;&lt;br /&gt;//The function that appends the Source url to links&lt;br /&gt;function appendSourceUrlToLinks()&lt;br /&gt;{&lt;br /&gt;//Collect all links on the page&lt;br /&gt;var links = document.getElementsByTagName("a");&lt;br /&gt;&lt;br /&gt;//Loop through the links&lt;br /&gt;for (var curLink=0; curLink &lt;&gt;0&lt;br /&gt;links[curLink].href.indexOf("EditForm.aspx")&gt;0&lt;br /&gt;links[curLink].href.indexOf("DispForm.aspx")&gt;0)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;//optional, ignore links that already have a source url&lt;br /&gt;if(links[curLink].href.indexOf("Source=") &lt;=0) { if(links[curLink].href.indexOf("?")&gt;0)&lt;br /&gt;links[curLink].href = links[curLink].href + "&amp;amp;Source=" + escape(window.location);&lt;br /&gt;&lt;br /&gt;else&lt;br /&gt;links[curLink].href = links[curLink].href + "?Source=" + escape(window.location);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;**************************************************************************&lt;br /&gt;&lt;br /&gt;Save this file in the following location on the SharePoint server:&lt;br /&gt;&lt;br /&gt;C:\Program Files\Common Files\Microsoft Shared\Web Server Extenstions\12\Template\Layouts\1033&lt;br /&gt;&lt;br /&gt;The final step is to add a javascript reference to the custom page via SharePoint Designer (unfortunately, I don't think there is any other way...).&lt;br /&gt;&lt;br /&gt;Find the following asp tag in the code of your custom page:&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#000099;"&gt;&amp;lt;asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server"&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Add the following JavaScript reference directly below it:&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#000099;"&gt;&amp;lt;script type="text/javascript" src="/_layouts/1033/custom_webpart_script.js"&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Save your page, and giv'r a go.&lt;br /&gt;&lt;br /&gt;That's it! Obviousely you can customize the code to your liking. Play around with it. I have also used a similar method to append other querystring arguments to pages that use a querystring filter.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3483401514643638890-4142215306657290139?l=wadehuntersblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://wadehuntersblog.blogspot.com/feeds/4142215306657290139/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3483401514643638890&amp;postID=4142215306657290139' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3483401514643638890/posts/default/4142215306657290139'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3483401514643638890/posts/default/4142215306657290139'/><link rel='alternate' type='text/html' href='http://wadehuntersblog.blogspot.com/2008/07/controlling-sharepoint-redirection-url.html' title='Controlling the SharePoint Redirection URL'/><author><name>Wade Hunter - B.Sc Computer Science, MCP, MCTS</name><uri>http://www.blogger.com/profile/10725503127402427147</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3483401514643638890.post-6647178421816483665</id><published>2008-07-23T07:29:00.000-07:00</published><updated>2008-07-23T07:45:43.531-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Intro'/><title type='text'>My First Blog</title><content type='html'>Hello!&lt;br /&gt;&lt;br /&gt;Finally my first blog!&lt;br /&gt;&lt;br /&gt;Hopefully I will find the time to post useful articles here.&lt;br /&gt;&lt;br /&gt;I am a software developper / technical analyst for a small software company in Ottawa, Ontario, Canada. We are a Gold Certified Microsoft partner, and I specialize in C# .net, and SharePoint development.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3483401514643638890-6647178421816483665?l=wadehuntersblog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://wadehuntersblog.blogspot.com/feeds/6647178421816483665/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3483401514643638890&amp;postID=6647178421816483665' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3483401514643638890/posts/default/6647178421816483665'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3483401514643638890/posts/default/6647178421816483665'/><link rel='alternate' type='text/html' href='http://wadehuntersblog.blogspot.com/2008/07/my-first-blog.html' title='My First Blog'/><author><name>Wade Hunter - B.Sc Computer Science, MCP, MCTS</name><uri>http://www.blogger.com/profile/10725503127402427147</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
