none
Conputed Field value not displaying on the List

    Question

  • Hello.

    I created 2 field schema; "myfieldid" and "myfield". "myfield" field value bases on "myfieldid" field value.

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Field ID="{C02E6B88-FAE2-46E5-B16D-A03EDE812D9A}"
          Name="myfieldid"
          SourceID="http://schemas.microsoft.com/sharepoint/v3"
          StaticName="myfieldid"
          Group="mygroup"
          Type="Text"
          DisplayName="My Field ID">
      </Field>
      <Field ID="{9CFC4BEC-C8FD-4222-BF27-4210EEB61F29}"
          Name="myfield"
          SourceID="http://schemas.microsoft.com/sharepoint/v3"
          StaticName="myfield"
          Group="mygroup"
          Type="Computed"
          DisplayName="My Field"
          ResultType="Text"
          ReadOnly="TRUE"
          ShowInDisplayForm="TRUE">
        <FieldRefs>
          <FieldRef ID="{C02E6B88-FAE2-46E5-B16D-A03EDE812D9A}" Name="myfieldid"/>
        </FieldRefs>
        <DisplayPattern>
          <Switch>
            <Expr>
              <Column Name="myfieldid"></Column>
            </Expr>
            <Case Value="ABC">
              <HTML><![CDATA[This is ABC]]></HTML>
            </Case>
            <Case Value="ABCABC">
              <HTML><![CDATA[This is ABCABC.]]></HTML>
            </Case>
            <Default>
              <HTML><![CDATA[Bad Value.]]></HTML>
            </Default>
          </Switch>
        </DisplayPattern>
      </Field>
    </Elements>
    

    "myfieldid" field value is displayed correctly, but "myfield" field value is not displayed on List.

    In Edit Form, the "myfield" value is displayed correctly.

    I cannot understand why "myfield" value is not displayed. Hope someone help me.

    Regards,

    Sunday, March 04, 2012 4:19 PM

Answers

  • After some study, I found a new XSL file is needed.

    When I add xsl file(below) to LAYOUTS/XSL folder, the value is displayed.

    fldtypes_myfile.xsl

    <xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema"
                    xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
                    version="1.0"
                    exclude-result-prefixes="xsl msxsl ddwrt"
                    xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
                    xmlns:asp="http://schemas.microsoft.com/ASPNET/20"
                    xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                    xmlns:SharePoint="Microsoft.SharePoint.WebControls"
                    xmlns:ddwrt2="urn:frontpage:internal">
      
      <xsl:template match ="FieldRef[@Name='MyField']" mode="Computed_body">
        <xsl:param name="thisNode" select="."/>
        
        <xsl:call-template name="DisplayMyField">
          <xsl:with-param name="thisNode" select="$thisNode"/>
        </xsl:call-template>
        
      </xsl:template>
    
      <xsl:template name="DisplayMyField">
        <xsl:param name="thisNode" select="."/>
        
        <xsl:choose>
          <xsl:when test="$thisNode/@MyFieldId='ABC'">
            This is ABC.
          </xsl:when>
          <xsl:when test="$thisNode/@MyFieldId='ABCABC'">
            This is ABCABC.
          </xsl:when>
          <xsl:otherwise>
            Bad Value.
          </xsl:otherwise>
        </xsl:choose>
        
      </xsl:template>
      
    </xsl:stylesheet>

    • Marked as answer by Mao Zhi Monday, March 05, 2012 8:31 AM
    Monday, March 05, 2012 8:31 AM

All replies

  • if you are using content type to add the fields then

    check the below link

    http://social.technet.microsoft.com/Forums/en-US/sharepoint2010customization/thread/77e60882-dec9-4bb9-b5b4-2524f42e8858/

    you try create a list  first

    http://devendra-sharepoint.blogspot.in/2012/01/creating-list-programatically-in_30.html

    and then proramatically add the calculated fields .


    MCTS,MCPD Sharepoint 2010. My Blog- http://sharepoint-journey.com


    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful


    Sunday, March 04, 2012 5:27 PM
  • Thanks, devendra

    I have added the fields programatically.

    On my list view, the "My Field" value is not displayed:

    On my view properties form, the "My Field" value is displayed:


    What I want to do is displaying the "My Field" value in list view.
    • Edited by Mao Zhi Monday, March 05, 2012 1:26 AM
    Monday, March 05, 2012 1:24 AM
  • could you please share the code ,so we are in a better position to solve the issue .

    MCTS,MCPD Sharepoint 2010. My Blog- http://devendra-sharepoint.blogspot.in
    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful

    Monday, March 05, 2012 2:54 AM
  • OK. Here is my code.

    My project has a element.xml shown above and a feature event receiver.

    I use the Feature Event Receiver to add, configure and delete document library.

    [Guid("c61d44cd-362d-4ede-ab6a-d1b4980af149")]
        public class Feature1EventReceiver : SPFeatureReceiver
        {
            public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                SPWeb web = properties.Feature.Parent as SPWeb;
                web.AllowUnsafeUpdates = true;
                web.Lists.Add("MyDocLibrary", "", SPListTemplateType.DocumentLibrary);
                web.Update();
                web.AllowUnsafeUpdates = false;
    
                SPDocumentLibrary doc = web.Lists["MyDocLibrary"] as SPDocumentLibrary;
                SPView view = doc.Views[0];
                Guid f1 = new Guid("{C02E6B88-FAE2-46E5-B16D-A03EDE812D9A}");
                Guid f2 = new Guid("{9CFC4BEC-C8FD-4222-BF27-4210EEB61F29}");
                SPField spf1 = web.Fields[f1];
                SPField spf2 = web.Fields[f2];
                web.AllowUnsafeUpdates = true;
                if (doc.Fields.Contains(f1) == false) { doc.Fields.Add(spf1); view.ViewFields.Add(spf1); }
                if (doc.Fields.Contains(f2) == false) { doc.Fields.Add(spf2); view.ViewFields.Add(spf2); }
                doc.OnQuickLaunch = true;
                view.Update();
                doc.Update();
                web.AllowUnsafeUpdates = false;
    
                web.Dispose();
            }
    
            public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
            {
                SPWeb web = properties.Feature.Parent as SPWeb;
                SPDocumentLibrary doc = (SPDocumentLibrary)web.Lists["MyDocLibrary"];
                web.AllowUnsafeUpdates = true;
                if (doc != null) doc.Delete();
                web.AllowUnsafeUpdates = false;
            }
        }

    After activating the feature, a file is uploaded to the "MyDocLibrary" manually.


    Regards,


    • Edited by Mao Zhi Monday, March 05, 2012 3:39 AM
    Monday, March 05, 2012 3:36 AM
  • After some study, I found a new XSL file is needed.

    When I add xsl file(below) to LAYOUTS/XSL folder, the value is displayed.

    fldtypes_myfile.xsl

    <xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema"
                    xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
                    version="1.0"
                    exclude-result-prefixes="xsl msxsl ddwrt"
                    xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
                    xmlns:asp="http://schemas.microsoft.com/ASPNET/20"
                    xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                    xmlns:SharePoint="Microsoft.SharePoint.WebControls"
                    xmlns:ddwrt2="urn:frontpage:internal">
      
      <xsl:template match ="FieldRef[@Name='MyField']" mode="Computed_body">
        <xsl:param name="thisNode" select="."/>
        
        <xsl:call-template name="DisplayMyField">
          <xsl:with-param name="thisNode" select="$thisNode"/>
        </xsl:call-template>
        
      </xsl:template>
    
      <xsl:template name="DisplayMyField">
        <xsl:param name="thisNode" select="."/>
        
        <xsl:choose>
          <xsl:when test="$thisNode/@MyFieldId='ABC'">
            This is ABC.
          </xsl:when>
          <xsl:when test="$thisNode/@MyFieldId='ABCABC'">
            This is ABCABC.
          </xsl:when>
          <xsl:otherwise>
            Bad Value.
          </xsl:otherwise>
        </xsl:choose>
        
      </xsl:template>
      
    </xsl:stylesheet>

    • Marked as answer by Mao Zhi Monday, March 05, 2012 8:31 AM
    Monday, March 05, 2012 8:31 AM