none
Extended CQWP's custom function

    Question

  • I extended a CQWP to invoke an c# function from xsl(ItemStyle.xsl)

    // This code is inside the extended CQWP class
    protected override void ModifyXsltArgumentList(ArgumentClassWrapper argList)
            {
                base.ModifyXsltArgumentList(argList);
                argList.AddExtensionObject("http://schemas.MyNamespace.nl/WebPartCode", MyNamespace.WebPartCode.CQWPCustomFunction.Instance);
               
            }
      
      And I have another class containing the function invoked from xsl
      
      public class CQWPCustomFunction
      {
       public static CQWPCustomFunction Instance
            {
                get
                {
                    if (_instance == null)
                    {
                        _instance = new CQWPCustomFunction();
                       
                    }
                    return _instance;
                }
            }
            private CQWPCustomFunction()
            {        
            }
            public string GetUserPicture(string name)
            {

                //get user picture
               
            }
      
      
    The function GetUserPicture() is called from a template inside ItemStyle.xsl and it gets invoked successfully(I have verified it by debugging)
    But inside the Webpart zone where my CQWP is I get an error "The Web Part cannot be displayed. You can troubleshoot this issue by opening the Web page in an HTML editor compatible with Windows SharePoint Services, such as. Microsoft Office Sharepoint Designer. Contact the Web server's administrator if the problem persists."

    The way I'm calling this function from xsl is ,
    <xsl:variable name="testvar">
       <xsl:value-of select="custom:GetUserPicture($Aname)"/>
      </xsl:variable>
    I've registered the namespace 'custom' inside the xsl.

    ALso if I call a parameter less function , I dont receive any error on the webpart.
    Like this ,<xsl:variable name="testvar">
       <xsl:value-of select="custom:test()"/>
      </xsl:variable>
    But my situation requires a function with parameter as I want the user picture.

    P.S. :This is in MOSS 2007.

      

      

     

    Tuesday, September 24, 2013 3:40 PM

Answers

  • Hi Ankit,

    after doing some research, tried to change to other column, such as employee ID,

    static Image GetUserPicture(string employeeId)
    {
      
    using (DirectorySearcher dsSearcher = new DirectorySearcher())
      
    {
          dsSearcher
    .Filter = "(&(objectClass=user) (employeeId=" + employeeId + "))"; //employeeId is the custom column name
         
    SearchResult result = dsSearcher.FindOne();

         
    using (DirectoryEntry user = new DirectoryEntry(result.Path))
         
    {
             
    byte[] data = user.Properties["jpegPhoto"].Value as byte[];

             
    if (data != null)
             
    {
                
    using (MemoryStream s = new MemoryStream(data))
                
    {
                    
    return Bitmap.FromStream(s);
                
    }
             
    } 

             
    return null;
         
    }
      
    }
    }

    from the testing, it return the bitmap, perhaps you can double check the code also,


    Regards,
    Aries
    Microsoft Online Community Support


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    • Marked as answer by Ankit_Girgas Tuesday, October 15, 2013 3:07 PM
    Tuesday, October 15, 2013 6:21 AM

All replies

  • Hi Ankit,

    I am trying to involve someone familiar with this topic to further look at this issue.

    Thanks,
    Daniel Yang
    Forum Support
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com. 


    Daniel Yang
    TechNet Community Support

    Wednesday, September 25, 2013 8:14 AM
  • Hi Ankit_Girgas,

    if you open your solution using Sharepoint Designer, do it have more clue regarding this issue?

    and check if the variable of $Aname, is passed correctly?

    if you have the xpath, perhaps you able to check this coding:

     </img></td>
    <td><img>
      <xsl:attribute name="src">
      <xsl:value-of select="image-clear2/@src" />
      </xsl:attribute>
      <xsl:attribute name="width">
      <xsl:value-of select="image-clear2/@width" />
      </xsl:attribute>
      <xsl:attribute name="height">
      <xsl:value-of select="image-clear2/@height" />
      </xsl:attribute>


    Regards,
    Aries
    Microsoft Online Community Support


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Wednesday, September 25, 2013 9:44 AM
  • Hi Aries,
    THe value of $Aname if getting passed correctly. I've verified it by debugging the code. THe $Aname value is getting passed correctly.
    Thursday, September 26, 2013 12:41 PM
  • Hi Ankit,

    if the parameters and the result is passed correctly, then, quite strange that the webpart is failing.

    please have a check this article, perhaps can give some clue,

    http://blog.mastykarz.nl/extending-content-query-web-part-xslt-custom-functions/

    if the article didnt do any good, i suggest you may need to open a case so that we can try your environment check if something is blocking it to be run.


    Regards,
    Aries
    Microsoft Online Community Support


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Friday, September 27, 2013 9:47 AM
  • You need your method to be static method should be:

    public static string GetUserPicture(string name)
    {

    }

    Tarek Yehia
    Blog:http://sharepointserver-2007.blogspot.com/


    Tarek Yehia

    Friday, September 27, 2013 1:24 PM
  • Tried that too. Still no success.

    As I said earlier, the method works if its a parameterless one.

    Thanks,

    Ankit GIrgas

    Tuesday, October 01, 2013 6:01 PM
  • Can you write code again (included all functions like test()) because I found your function not static

    Tarek Yehia

    Tuesday, October 01, 2013 6:30 PM
  • Hi Ankit,

    after doing some research, tried to change to other column, such as employee ID,

    static Image GetUserPicture(string employeeId)
    {
      
    using (DirectorySearcher dsSearcher = new DirectorySearcher())
      
    {
          dsSearcher
    .Filter = "(&(objectClass=user) (employeeId=" + employeeId + "))"; //employeeId is the custom column name
         
    SearchResult result = dsSearcher.FindOne();

         
    using (DirectoryEntry user = new DirectoryEntry(result.Path))
         
    {
             
    byte[] data = user.Properties["jpegPhoto"].Value as byte[];

             
    if (data != null)
             
    {
                
    using (MemoryStream s = new MemoryStream(data))
                
    {
                    
    return Bitmap.FromStream(s);
                
    }
             
    } 

             
    return null;
         
    }
      
    }
    }

    from the testing, it return the bitmap, perhaps you can double check the code also,


    Regards,
    Aries
    Microsoft Online Community Support


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    • Marked as answer by Ankit_Girgas Tuesday, October 15, 2013 3:07 PM
    Tuesday, October 15, 2013 6:21 AM