Monday, August 1, 2011

Creating Custom SharePoint fields with Display Pattern Element using CAML in C#

 

I got an idea to create a image field and see below what I did for this…..Smile

clip_image002[4]

The basic idea behind is to create…… Conditional population of the images.

The code goes here.

private void btn5_Click(object sender, EventArgs e)

        {

            const string strListName = "Bugs1";

            string strUrl = "http://myserver004:7010/sites/MS/";

            SPSite oSite = new SPSite(strUrl);

            SPWeb oWeb = oSite.OpenWeb();

            //// Add  the list

            Guid ConfigListID = oWeb.Lists.Add(strListName,"Configuration List", SPListTemplateType.GenericList);

            oWeb.Update();

            SPList oList = oWeb.Lists[ConfigListID];

            ////Adding the status field, based on which other 3 fields would work

            string strFieldSchema = GetStatusSchemaXml();

            oList.Fields.AddFieldAsXml(strFieldSchema, true, SPAddFieldOptions.AddFieldToDefaultView);

            ////Adding calculated fields to the list - Status, Active, Resolve, Close

            strFieldSchema = GetActiveFieldSchemaXml1();

            oList.Fields.AddFieldAsXml(strFieldSchema, true, SPAddFieldOptions.AddFieldToDefaultView);

 

            strFieldSchema = GetResolveFieldSchemaXml1();

            oList.Fields.AddFieldAsXml(strFieldSchema, true, SPAddFieldOptions.AddFieldToDefaultView);

 

            strFieldSchema = GetCloseFieldSchemaXml1();

            oList.Fields.AddFieldAsXml(strFieldSchema, true, SPAddFieldOptions.AddFieldToDefaultView);

 

            oList.Update();

        }

Now the corresponding methods for adding custom fields with image and condition in CAML

private string GetStatusSchemaXml()

        {

            string strFieldSchema = string.Empty;

            strFieldSchema = @"<Field ID='{CF2BA2DD-5F34-4df0-8C7B-64744387C214}'

                Name='Status'

                Format='Dropdown'

                StaticName='Status'

                Group='CF'

                Required='FALSE'

                Type='Choice'

                DisplayName='Status' >

                    <CHOICES>

                      <CHOICE>Active</CHOICE>

                      <CHOICE>Resolved</CHOICE>

                      <CHOICE>Closed</CHOICE>

                    </CHOICES>

                    <Default>Active</Default>

                </Field>";

            return strFieldSchema;

        }

#region Adding Custom Image Link Fields OOB Page

        //Active

        private string GetActiveFieldSchemaXml1()

        {

           string strFieldSchema = string.Empty;

           strFieldSchema = @"<Field ID='{DF1EF516-C196-41b3-9B12-F22A34891403}'

          Name='Active'

          Group='CF'

          Type='Computed'

          ResultType='text'

          Sortable='FALSE'

          Filterable='FALSE'

          Hidden='FALSE'

          DisplayName='Activate'

          ClassInfo='Icon'

          AuthoringInfo='$Resources:core,Linked_Item;'>

        <FieldRefs>

          <FieldRef ID='{94f89715-e097-4e8b-ba79-ea02aa8b7adb}' Name='FileRef'/>

          <FieldRef ID='{CF2BA2DD-5F34-4df0-8C7B-64744387C214}' Name='Status'/>

          <FieldRef Name='ID' />

        </FieldRefs>

        <DisplayPattern>

                  <IfEqual>

                    <Expr1>Active</Expr1>

                    <Expr2><Field Name='Status'/></Expr2>

                    <Then></Then>

                    <Else>

                      <HTML><![CDATA[<a href=']]></HTML>

                      <HttpHost />

                      <UrlDirName>

                      <HTML>/</HTML>

                      <LookupColumn URLEncodeAsURL='TRUE' Name='FileRef' />

                      </UrlDirName>

                      <HTML><![CDATA[/EditForm.aspx?ID=]]></HTML>

                     <Column HTMLEncode='TRUE' Name='ID'></Column>

                        <HTML><![CDATA[&amp;List=]]></HTML>

                        <ListProperty Select='Name' URLEncode='True'/>

                      <HTML><![CDATA[' onclick='GoToLink(this);return false;' target='_self'>]]></HTML>

                      <HTML><![CDATA[<img border='0' alt=']]></HTML>

                      <HTML>Activate</HTML>

                      <HTML><![CDATA[' src=']]></HTML>

                      <ImagesPath />

                      <HTML><![CDATA[IMNBUSY.PNG'>]]></HTML>

                      <HTML><![CDATA[</a>]]></HTML>

                    </Else>

                  </IfEqual>

                </DisplayPattern>

      </Field>";

            return strFieldSchema;

        }

        //Resolve

        private string GetResolveFieldSchemaXml1()

        {

            string strFieldSchema = string.Empty;

            strFieldSchema = @"<Field ID='{C0CF70C0-810F-4fc5-86BF-EBC4C45F15B0}'

           Name='Resolve'

           Group='CF'

           Type='Computed'

           Sortable='FALSE'

           Filterable='FALSE'

           Hidden='FALSE'

           DisplayName='Resolve'

           ClassInfo='Icon'

           AuthoringInfo='$Resources:core,Linked_Item;'>

            <FieldRefs>

              <FieldRef ID='{94f89715-e097-4e8b-ba79-ea02aa8b7adb}' Name='FileRef'/>

              <FieldRef ID='{CF2BA2DD-5F34-4df0-8C7B-64744387C214}' Name='Status'/>

              <FieldRef Name='ID' />

            </FieldRefs>

            <DisplayPattern>

                  <IfEqual>

                    <Expr1>Active</Expr1>

                    <Expr2><Field Name='Status'/></Expr2>

                    <Then>

                      <HTML><![CDATA[<a href=']]></HTML>

                      <HttpHost />

                      <UrlDirName>

                      <HTML>/</HTML>

                      <LookupColumn URLEncodeAsURL='TRUE' Name='FileRef' />

                      </UrlDirName>

                      <HTML><![CDATA[/EditForm.aspx?ID=]]></HTML>

                       <Column HTMLEncode='TRUE' Name='ID'></Column>

                        <HTML><![CDATA[&amp;List=]]></HTML>

                        <ListProperty Select='Name' URLEncode='True'/>

                        <HTML><![CDATA[' onclick='GoToLink(this);return false;' target='_self'>]]></HTML>

                      <HTML><![CDATA[<img border='0' alt=']]></HTML>

                      <HTML>Resolve</HTML>

                      <HTML><![CDATA[' src=']]></HTML>

                      <ImagesPath />

                      <HTML><![CDATA[IMNAWAY.PNG'>]]></HTML>

                      <HTML><![CDATA[</a>]]></HTML>

                    </Then>

                    <Else></Else>

                  </IfEqual>

            </DisplayPattern>

              </Field>";

            return strFieldSchema;

        }

        //Close

        private string GetCloseFieldSchemaXml1()

        {

            string strFieldSchema = string.Empty;

            strFieldSchema = @"<Field ID='{90347BCF-BF51-49aa-BA86-F28CBF3CCA5D}'

           Name='Close'

           Group='CF'

           Type='Computed'

           Sortable='FALSE'

           Filterable='FALSE'

           Hidden='FALSE'

           DisplayName='Close'

           ClassInfo='Icon'

           AuthoringInfo='$Resources:core,Linked_Item;'>

            <FieldRefs>

              <FieldRef ID='{94f89715-e097-4e8b-ba79-ea02aa8b7adb}' Name='FileRef'/>

              <FieldRef ID='{CF2BA2DD-5F34-4df0-8C7B-64744387C214}' Name='Status'/>

              <FieldRef Name='ID' />

            </FieldRefs>

            <DisplayPattern>

                  <IfEqual>

                    <Expr1>Resolved</Expr1>

                    <Expr2><Field Name='Status'/></Expr2>

                    <Then>

                      <HTML><![CDATA[<a href=']]></HTML>

                        <HttpHost />

                      <UrlDirName>

                      <HTML>/</HTML>

                      <LookupColumn URLEncodeAsURL='TRUE' Name='FileRef' />

                      </UrlDirName>

                      <HTML><![CDATA[/EditForm.aspx?ID=]]></HTML>

                        <Column HTMLEncode='TRUE' Name='ID'></Column>

                        <HTML><![CDATA[&amp;List=]]></HTML>

                        <ListProperty Select='Name' URLEncode='True'/>

                        <HTML><![CDATA[' onclick='GoToLink(this);return false;' target='_self'>]]></HTML>

                      <HTML><![CDATA[<img border='0' alt=']]></HTML>

                        <HTML>Close</HTML>

                        <HTML><![CDATA[' src=']]></HTML>

                        <ImagesPath />

                        <HTML><![CDATA[IMNON.PNG'>]]></HTML>

                      <HTML><![CDATA[</a>]]></HTML>

                    </Then>

                    <Else></Else>

                  </IfEqual>

            </DisplayPattern>

          </Field>";

            return strFieldSchema;

        }

        #endregion

 

Let us see how does it look like after creating the list with the above code and adding fields

clip_image004[4]

 

On click on the images the edit form opens

clip_image006[4]

Tool tip

clip_image008[4]

In case if you use custom forms for the list then use this modified code to accommodate the custom pages. The code mentioned below shows the custom code for active field. It is considered that the custom pages and images are already deployed in the 12 hive. This method creates the same functionality. The attributes to be noted here are.

  private string GetActiveFieldSchemaXml()

        {

           string strFieldSchema = string.Empty;

           strFieldSchema = @"<Field ID='{DF1EF516-C196-41b3-9B12-F22A34891403}'

          Name='Active'

          Group='CF'

          Type='Computed'

          ResultType='text'

          Sortable='FALSE'

          Filterable='FALSE'

          Hidden='FALSE'

          DisplayName='Activate'

          ClassInfo='Icon'

          AuthoringInfo='$Resources:core,Linked_Item;'>

        <FieldRefs>

          <FieldRef ID='{94f89715-e097-4e8b-ba79-ea02aa8b7adb}' Name='FileRef'/>

          <FieldRef ID='{CF2BA2DD-5F34-4df0-8C7B-64744387C214}' Name='Status'/>

          <FieldRef Name='ID' />

        </FieldRefs>

        <DisplayPattern>

                  <IfEqual>

                    <Expr1>Active</Expr1>

                    <Expr2><Field Name='Status'/></Expr2>

                    <Then></Then>

                    <Else>

                      <HTML><![CDATA[<a href=']]></HTML>

                      <HttpVDir URLEncodeAsURL='TRUE' />

                      <HTML><![CDATA[/_Layouts/Tracker/Pages/ViewFormT.aspx?ID=]]></HTML>

                     <Column HTMLEncode='TRUE' Name='ID'></Column>

                        <HTML><![CDATA[&amp;List=]]></HTML>

                        <ListProperty Select='Name' URLEncode='True'/>

                      <HTML><![CDATA[' onclick='GoToLink(this);return false;' target='_self'>]]></HTML>

                      <HTML><![CDATA[<img border='0' alt=']]></HTML>

                      <HTML>Activate</HTML>

                      <HTML><![CDATA[' src=']]></HTML>

                      <ImagesPath />

                      <HTML><![CDATA[Tracker.Images/IMNBUSY.GIF'>]]></HTML>

                      <HTML><![CDATA[</a>]]></HTML>

                    </Else>

                  </IfEqual>

                </DisplayPattern>

      </Field>";

            return strFieldSchema;

        }

If you have a more elegant solution – please post a comment… I’ll be happy to hear.

...HaPpY CoDiNg

Partha (Aurum)

Reference:

http://msdn.microsoft.com/en-us/library/ms469877.aspx

http://207.46.16.248/en-us/library/dd588138(office.11).aspx

No comments:

Post a Comment