Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharepoint Guidfield - how to display it in frontend and set its value in code?

On a custom form of mine I want to display a custom Guid field.

I created the field the usual way via a feature:

<Field ID="88813c02-799b-4fc8-8ed8-72fe668e257c" Type="Guid"
                                                 Name="myGuid"
                                                 StaticName="myGuid"
                                                 DisplayName="My Guid" />

This field I want first set via code and display on a form. On the form I have the following control (for reference I also include a title field):

<SharePoint:GuidField runat="server" ID="myGuidField" FieldName="myGuid" />
<SharePoint:TextField runat="server" ID="myTitle" FieldName="Title" />

The Guid field doesn't display on a regular New/Edit form - it's simply empty. In the code behind of the custom form I can do the following:

myTitle.Value = "Some Title Value"; 
string testValue = myTitle.Value; //-->"Some Title Value"

but when trying to set the value of the Guid field - it somehow cannot be done:

string anotherValue = myGuidField.Value; //--> null
Guid myGuid = new Guid("7f824c3f-4049-4034-a231-85291fce2680");
myGuidField.Value = myGuid;
string anotherValue = myGuidField.Value; //--> still null
//but myGuidField is seen as a "Microsoft.Sharepoint.WebControls.GuidField"

So somehow I'm just not able to set the Guid value programmatically and I'm neither able to only display the Guid.

Two questions:

  1. How to display a GuidField (the GUID it contains) on a form?
  2. How to set the value of a GuidField
like image 516
Dennis G Avatar asked Feb 24 '23 20:02

Dennis G


1 Answers

GuidField do not override property Value of BaseFieldControl and this property always return null and nothing set for this field control. Default implementation of GuidField doesn't have any DisplayTemplates and if you place this field control on the custom or even standard form - field will not render any html elements for setting value. Value of the field you should set manually. To set custom value use ItemFieldValue:

myGuidField.ItemFieldValue = new Guid("7f824c3f-4049-4034-a231-85291fce2680");

or just

item["myGuid"] =  new Guid("7f824c3f-4049-4034-a231-85291fce2680"); 

If you want to edit guid on the UI form(but in most cases it's not needed) write small custom field control or try to search on the codeplex(as I know there is no out of the box solutions).

like image 181
Ilya Avatar answered Apr 26 '23 16:04

Ilya