{"id":1127,"date":"2012-08-15T20:41:17","date_gmt":"2012-08-15T19:41:17","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=1127"},"modified":"2012-08-15T20:41:17","modified_gmt":"2012-08-15T19:41:17","slug":"getdatatable-object-reference-error","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2012\/08\/15\/getdatatable-object-reference-error\/","title":{"rendered":"GetDataTable Object Reference Error"},"content":{"rendered":"<p>Calling the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.splistitemcollection.getdatatable.aspx\" target=\"_blank\">GetDataTable<\/a> method of a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.splistitemcollection.aspx\" target=\"_blank\">SPListItemCollection<\/a> object that was retrieved using a join with <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spquery.projectedfields.aspx\" target=\"_blank\">projected fields<\/a> will throw the following error:<\/p>\n<p><em>Object reference not set to an instance of an object<\/em><\/p>\n<p>For example:<\/p>\n<pre lang=\"csharp\">\r\nSPQuery query = new SPQuery();\r\nquery.Query = \"<Where><Eq><FieldRef Name=\"Title\"\/><Value Type=\"Text\">Search Criteria<\/Value><\/Eq><\/Where>\";\r\nquery.Joins = \"<Join Type=\"Left\" ListAlias=\"Name of join list\"><Eq><FieldRef Name=\"Title\" RefType=\"Id\" \/><FieldRef List=\"Name of join list\" Name=\"Id\" \/><\/Eq><\/Join>\";\r\nquery.ProjectedFields = \"<Field Name='Field name' Type='Lookup' List='Name of join list' ShowField='Field name'\/>\";\r\nquery.ViewFields = \"<FieldRef Name=\"Field Name\" \/><FieldRef Name=\"Title\" \/>\";\r\n\r\n\/\/ Next line will cause a \"Object reference not set to an instance of an object\" exception.\r\nDataTable table = listItems.GetDataTable();\r\n<\/pre>\n<p>Simply access the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spfieldcollection.aspx\" target=\"_blank\">Fields<\/a> property prior to calling <em>GetDataTable<\/em> to resolve this issue:<\/p>\n<pre lang=\"csharp\">\r\nSPFieldCollection fields = listItems.Fields;\r\nDataTable table = listItems.GetDataTable();\r\n<\/pre>\n<p>Accessing the <em>Fields<\/em> property instantiates an object that is used by the <em>GetDataTable<\/em> method when there are projected fields in the join and the exception is resolved.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Calling the GetDataTable method of a SPListItemCollection object that was retrieved using a join with projected fields will throw the following error: Object reference not set to an instance of an object For example: SPQuery query = new SPQuery(); query.Query &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2012\/08\/15\/getdatatable-object-reference-error\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[3],"tags":[81],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-ib","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1127"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=1127"}],"version-history":[{"count":10,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1127\/revisions"}],"predecessor-version":[{"id":1138,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1127\/revisions\/1138"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=1127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=1127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=1127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}