Wrox Programmer Forums
BOOK: Professional Business Connectivity Services in SharePoint 2010 by Scot Hillier, Brad Stevenson
This is the forum to discuss the Wrox book Professional Business Connectivity Services in SharePoint 2010 by Scot Hillier, Brad Stevenson; ISBN: 978-0-470-61790-8
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Business Connectivity Services in SharePoint 2010 by Scot Hillier, Brad Stevenson section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 1st, 2013, 06:34 PM
Authorized User
Join Date: Jul 2010
Posts: 21
Thanks: 1
Thanked 0 Times in 0 Posts
Default Querying an external list.

Hey all...
I'm attempting to grab the BDCIdentity value from my PartLookup field (lookup to an external list) and retrieve the SPListItem that corresponds to this site column's value on the current SPListItem in the child list. You figure that would be fairly simple. It is turning out to be quite painful.
I tried to use a bunch of different approaches and none seem to work.

So, the goal actually is in an ItemAdding event receiver, for the current affected SPListItem, grab the BDCIdentity value (ie '__bg#######) and locate the corresponding SPListItem in the external list. I actually need to retrieve another field value for that list.

Anyway, here is the final version of my code and I'm in a simple console app. I'm logged into the SharePoint Server as the farm account when I'm executing the code.

        static void Main(string[] args)
            using (SPSite site = new SPSite(_sSiteURL))
                using (SPWeb web = site.OpenWeb())
                    Console.WriteLine("Site: " + site.Url);

                    SPList partList = null;

                    foreach (SPList lList in web.Lists)
                        if (lList.RootFolder.Name == "PartList")
                            partList = lList;
                    SPQuery query = new SPQuery();
                    query.Method = "<Method Name='GetAllPartEntitys'><Filter Name='Description' Value='{0}'/></Method>";
                    query.ViewFields = "<FieldRef Name='BdcIdentity'/><FieldRef Name='sName'/>";
                    query.Query = "<Where><Contains><FieldRef Name='sName'/><Value Type='Text'>HP</Value></Contains></Where><OrderBy><FieldRef Name='BdcIdentity'/></OrderBy>";
                    SPListItemCollection foundItems = partList.GetItems(query);
                    Console.WriteLine(String.Format("Count of FoundItems: {0}", foundItems.Count));




When the code gets to the final Console.WriteLine, it throws the following exception:

System.UnauthorizedAccessException was unhandled
  Message=Attempted to perform an unauthorized operation.
       at Microsoft.SharePoint.SPListDataSource.CheckUserIsAuthorized(SPBasePermissions perms)
       at Microsoft.SharePoint.SPListDataSource.GetFilteredEntityInstances(XmlDocument xdQueryView, Boolean fFormatDates, Boolean fUTCToLocal, String firstRowId, Boolean fBackwardsPaging, String& bdcidFirstRow, String& bdcidNextPageRow, List`1& lstColumnNames, Dictionary`2& dictColumnsUsed, List`1& mapRowOrdering, List`1& lstEntityData)
       at Microsoft.SharePoint.SPListItemCollection.EnsureEntityDataViewAndOrdering(String& bdcidFirstRow, String& bdcidNextPageFirstRow)
       at Microsoft.SharePoint.SPListItemCollection.EnsureListItemsData()
       at Microsoft.SharePoint.SPListItemCollection.get_Count()
       at SetExternalLookupField.Program.Main(String[] args) in C:\source\Blackcreek\SharePointUtils\SetExternalLookupField\SetExternalLookupField\Program.cs:line 66
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
I can browse the rows in external list without a problem. This same code will also not run under the event receiver.

Please note that I also attempted to use the caml syntax from Page 143 in the book. Same result.

Also, the final iteration of the approach I attempted is from this site.http://sharepoint.stackexchange.com/...-finder-method

Last edited by richv; February 1st, 2013 at 06:36 PM.. Reason: Added more detail.

Similar Threads
Thread Thread Starter Forum Replies Last Post
AddDependentLookup and external list lookup columns. richv BOOK: Professional Business Connectivity Services in SharePoint 2010 by Scot Hillier, Brad Stevenson 0 February 1st, 2013 02:45 PM
Problem setting the value of an external list lookup column. richv BOOK: Professional Business Connectivity Services in SharePoint 2010 by Scot Hillier, Brad Stevenson 2 January 25th, 2013 12:20 PM
The created external list does not support updating existing items Treffe BOOK: Beginning SharePoint 2010 Development 5 August 2nd, 2010 11:56 AM
Permissions issue on Create External List - resolved kendo666 BOOK: Beginning SharePoint 2010 Administration: Windows SharePoint Foundation 2010 and Microsoft Sha 0 July 27th, 2010 09:26 AM
Querying external Excel datafile from excel utalwalk Excel VBA 1 March 30th, 2006 12:47 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.