{"id":1172,"date":"2012-09-26T17:57:52","date_gmt":"2012-09-26T16:57:52","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=1172"},"modified":"2012-09-26T17:57:52","modified_gmt":"2012-09-26T16:57:52","slug":"manually-set-form-field-context","status":"publish","type":"post","link":"http:\/\/www.stuartroberts.net\/index.php\/2012\/09\/26\/manually-set-form-field-context\/","title":{"rendered":"Manually Set Form Field Context"},"content":{"rendered":"<p>When you&#8217;re developing custom code for SharePoint, sometimes you&#8217;ll have a situation where you&#8217;re dynamically adding form field controls to your control or WebPart and the form context will not be automatically picked up by the controls.  If you&#8217;re using them outside a new, display or edit form for example.<\/p>\n<p>It&#8217;s relatively simple to force the control(s) to the desired context but there&#8217;s something you should be aware of.<\/p>\n<p>Take a look the following code:<br \/>\n<!--more--><\/p>\n<pre lang=\"csharp\">\r\nprivate void SetFormFieldContext(FieldMetadata formfield, Guid listId, int itemId)\r\n{\r\n  formfield.ListId = listId;\r\n  formfield.ControlMode = ControlMode;\r\n  \r\n  \/\/ Get the SPContext object based on the current web and specified list id and item id. \r\n  SPContext context = SPContext.GetContext(HttpContext.Current, itemId < 0 ? 0 : itemId, listId, SPContext.Current.Web);\r\n  formfield.RenderContext = context;\r\n  formfield.ItemContext = context;\r\n  \r\n  if (ControlMode == SPControlMode.Display || ControlMode == SPControlMode.Edit)\r\n  {\r\n      formfield.ItemId = itemId;\r\n  }\r\n}\r\n<\/pre>\n<p>The important part is to manually set the form field's ListId property, as this is not updated after setting RenderContext or ItemContext as you might expect.<\/p>\n<p>The same applies to the ItemId property.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you&#8217;re developing custom code for SharePoint, sometimes you&#8217;ll have a situation where you&#8217;re dynamically adding form field controls to your control or WebPart and the form context will not be automatically picked up by the controls. If you&#8217;re using &hellip; <a href=\"http:\/\/www.stuartroberts.net\/index.php\/2012\/09\/26\/manually-set-form-field-context\/\">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":[54,81],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-iU","_links":{"self":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1172"}],"collection":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=1172"}],"version-history":[{"count":15,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1172\/revisions"}],"predecessor-version":[{"id":1190,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1172\/revisions\/1190"}],"wp:attachment":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=1172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=1172"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=1172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}