RSS Feed



    Social Links


    Archives

    March 2012 (3)
    February 2012 (2)
    January 2012 (4)
    December 2011 (2)
    October 2011 (4)
    September 2011 (1)
    August 2011 (1)
    June 2011 (2)
    May 2011 (1)
    March 2011 (6)
    February 2011 (3)
    January 2011 (4)
    November 2010 (2)
    October 2010 (2)
    September 2010 (3)
    August 2010 (3)
    July 2010 (4)
    June 2010 (2)
    May 2010 (2)
    March 2010 (5)
    January 2010 (4)
    December 2009 (1)
    November 2009 (3)
    October 2009 (3)
    September 2009 (1)
    August 2009 (3)
    July 2009 (2)
    June 2009 (2)
    May 2009 (1)
    April 2009 (2)
    March 2009 (1)
    January 2009 (1)
    December 2008 (5)
    November 2008 (3)
    October 2008 (5)
    September 2008 (2)
    August 2008 (2)
    July 2008 (1)
    June 2008 (3)
    April 2008 (4)
    March 2008 (4)
    February 2008 (3)
    January 2008 (6)
    December 2007 (8)
    November 2007 (12)
    October 2007 (4)
    September 2007 (3)
    August 2007 (5)
    July 2007 (18)

    Categories

    Recent Posts

    Other Blogs

    Stefan Goßner
    Tobias Zimmergren's thoughts on development
    Tom Van Rousselt's Blog
    Andrew Connell [MVP SharePoint]
    Chris O'Brien
    harbar.net
    IT Pro Ramblings
    Jan Tielens' Bloggings
    JOPX on SharePoint 2007 and SharePoint 2010
    Waldek Mastykarz
    Karine Bosch's Blog
    Mark Harrison 2010
    Michaël's coding thoughts
    Microsoft SharePoint Team Blog
    PDT IT Services Blog Posts
    Tom's Random Ranting
    Sebastian Bouckaert's Blog
    SharePoint Automation
    SharePoint Joel's SharePoint Land
    ]]> RED
    ]]>

    BROWN
    ]]>

    2 Comments

    Ellipsis on list item body using jQuery

    October 1, 2009 - 22:35, by Steven Van de Craen
    Categories: Custom Field Types, jQuery/JavaScript, MOSS 2007, Search Server 2008, SharePoint 2007, WSS 3.0, Advanced Computed Field

    A common question to receive is on list items with a long body (eg. Announcements) and then show only X characters and optionally a ‘read more’ link. Many ways to solve this but I went for the following: a Custom Field Type that renders the short text with ellipsis using jQuery.

    This is another example using the Advanced Computed Field in a creative way. The ACF allows you to create computed fields referencing other fields or data in your list and manipulating them through CAML or JavaScript. It has the advantage that this can be done from within the browser, however I admit CAML isn’t the easiest of things to comprehend in the SharePoint technology stack.

    Ellipsis sample

    This ellipsis displays the first 20 characters of the plain text version of the Announcement Body

     
        
        


         <span style="display:none;" id="el_
        
         _
        
         ">
        
         </span>
        
         <script type="text/javascript">
         $(function()
         { var mySpan = $('#el_
        
         _
        
         ');
           var myShortText = $(mySpan).text().substring(0, 20); 
           if ($(mySpan).text().length > 20)
             myShortText += '...';

           $(mySpan).show().html(myShortText); });
         </script>

    Notes

    0 Comments

    Advanced Computed Field

    March 31, 2009 - 21:34, by Steven Van de Craen
    Categories: .NET, SharePoint 2007, Search Server 2008, Custom Field Types, MOSS 2007, WSS 3.0, Advanced Computed Field

    Introduction

    This project originally started as ‘TitleLinkField’ because I needed a way to display the Title field as a hyperlink to the document in a document library, but it ended up being more than just that so I chose a more generic name for it.

    I had some experience with Custom Field Types but event then I spent too many hours (even days) on figuring this one out. It started with standard functionality such as Calculated Field and Computed Field, both having their flaws and limitations. I quickly realised that Custom Field Types might be the only way to tackle the scenario at hand.

    Use

    When creating a field based on this type (“Advanced Computed Field”) you need to provide two properties; FieldRefs (MSDN FieldRefs Element (List)) and DisplayPattern (MSDN: DisplayPattern Element (List)). The former requires a list of referenced fields that exist in the collection of our field while the latter contains CAML used for displaying our field. See the MSDN pages on both elements for the schema and possible values.

    What better way than demonstrating by showing ?

    Sample 1: Title linked to document


        
        


        
        
        
        
         ]]>
        
         ]]>

    Sample 2: Title with ECB


        
        
        


        
            
                 
             

             
                   -
             

             
                  
                  
                  
                  
                  
             

        

    Sample 3: Random formatting


        


         ]]>
        
         ]]>

    You can learn a lot by examining the default fields in a document library or list. A tool such as Stramit SharePoint 2007 Caml Viewer is invaluable here.

    Technical

    A Custom Field Types derives from a parent class (MSDN: Custom Field Classes). Furthermore you have to define the ParentType field declaratively in the FieldTypes XML definition. It is advised (maybe even required) for them to be the same type.

    Choose the ‘parent class’ that most closely matches your requirements. If you need a ‘text field with regular expression validation’ then go for SPFieldText because that has a lot of textbox relation functionality (especially on the New, Edit and Display Forms).

    The ‘ParentType’ field seems to be the key for CAML related functionality (such as the AllItems View). In my case setting it to “Text” meant that my field didn’t retrieve the values of the referenced fields so it needed to be “Computed”.

    The main issues with the standard Computed Field is that it is invisible from any view; Site Columns, Field in a Content Type, etc. and can only be created declaratively via XML/CAML or through code. Other than these issues the Computed Field did exactly what was needed; render columns in a way that I wanted via the RenderPattern. It turns out that the visibility of a Custom Field Type can be controlled in the declarative XML via the ”ShowOn…” fields.

    For storing and retrieving the DisplayPattern and FieldRefs properties my custom field type exposes two methods that make calls to internal SharePoint methods:

    public void SetInnerXmlForNode(string nodeName, string innerXml)
    {
         Type fldType = this.GetType();
         XmlDocument doc = new XmlDocument();
         doc.LoadXml(String.Format("<{0}>{1}", nodeName, innerXml));

         MethodInfo miSetXml = fldType.GetMethod("SetInnerXmlForNode", BindingFlags.Instance | BindingFlags.NonPublic);
         miSetXml.Invoke(this, new object[] { nodeName, doc.DocumentElement.InnerXml });
    }

    public string GetInnerXmlForNode(string nodeName)
    {
         string result = null;
         Type fldType = this.GetType();

         MethodInfo miGetXml = fldType.GetMethod("GetNodeFromXmlDom", BindingFlags.Instance | BindingFlags.NonPublic);
         XmlNode resultNode = miGetXml.Invoke(this, new object[] { nodeName }) as XmlNode;
         if (resultNode != null)
         {
              StringBuilder sb = new StringBuilder();
              XmlWriterSettings settings = new XmlWriterSettings();
              settings.Indent = true;
              settings.ConformanceLevel = ConformanceLevel.Fragment;
              using (XmlWriter writer = XmlTextWriter.Create(sb, settings))
              {
                   resultNode.WriteContentTo(writer);
              }

              result = sb.ToString();
         }
         return result;
    }

    Download and installation

    If you’re interested in reusing or modifying the code feel free to do so. If you just want this installed and available on your SharePoint farm then go for the WSP and deploy via STSADM.

    STSADM -o addsolution -filename VNTG.CustomFieldTypes.AdvancedComputedField.wsp
    STSADM -o deploysolution -name VNTG.CustomFieldTypes.AdvancedComputedField.wsp -allowgacdeployment -immediate -allcontenturls

    Word of caution

    There isn’t a lot of validation on the input of the XML properties. It has to be valid XML but that’s about it. Please follow the schema for FieldRefs and DisplayPattern to make sure you don’t break other functionality. It cannot ‘bring down the farm’ or anything, but it could definitely mess up the rendering of the View.

    Have fun with it !

    24 Comments