1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / filter for content with no parent

filter for content with no parent

10 posts, 0 answered
  1. Kyle
    Kyle avatar
    6 posts
    Registered:
    18 Jun 2009
    27 Sep 2009
    Link to this post
    Hi there I am trying to override CreateDataSource in a control inheriting contentview in order to filter it.  I only want it to display items that have no parent.  Looking at the database this would mean that parentid should be equal to null, so I have overriden GetFilterBuilder to return a filter where parentid is null but this doesnt work.  I get an exception from
    ParseParentsFilter() with the message  

    "Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)."


    The other method I have tried is setting it to filter on a guid equalling guid.empty
    however this does just that and returns no results as no items have a parentid =
    '000000-0000.....'

    Am I building the filter for this wrong as I just cant seem to make this work.

        protected override IList CreateDataSource() 
            { 
                ContentFilterBuilder filterBuilder = this.GetFilterBuilder(); 
                if (filterBuilder.IsFilterValid) 
                    return this.Manager.GetContent(base.GetContentStartIndex(), base.GetPageSize(), base.SortExpression, 
                                              filterBuilder.ParseTagFilter(), ContentStatus.Published, null, 
                                              filterBuilder.ParseParentsFilter(), filterBuilder.ParseMetaFieldsFilter()); 
     
                return base.CreateDataSource(); 
            }  
     
                    protected override ContentFilterBuilder GetFilterBuilder()  
            {  
                var filter =  base.GetFilterBuilder();  
                  
                // we only retrieve case studies without a parent  
      
                // method 1  
                filter.AddFilter(new ContentFilterStatement("ParentID",  
                        ContentFilter.Condition.Null ));  
     
                //method 2  
    //          filter.AddFilter(new ContentFilterStatement("ParentID",  
    //                        Guid.Empty.ToString(),  
    //                    ContentFilter.Condition.Equal,  
    //                    ContentFilter.JoinType.And));  
                return filter;  
            }  


  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    28 Sep 2009
    Link to this post
    Hi Kyle,

    You do not need to override the Filter or use ParseParentsFilter method. Generally the content items from - GenericContent, News, Events does not have parent.

    You can use the following filter that will return all items that does not have parent

      protected override System.Collections.IList CreateDataSource() 
        { 
            IList listofAllitems = this.Manager.GetContent(); 
            ContentFilterBuilder filter = new ContentFilterBuilder(this); 
            filter.AddFilter(new ContentFilterStatement("ParentID", ContentFilter.Condition.Null)); 
            return base.CreateDataSource(); 
        } 

    If you use Generic_Content, News or Events providers, the control will show nothing.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Kyle
    Kyle avatar
    6 posts
    Registered:
    18 Jun 2009
    28 Sep 2009
    Link to this post
    Hi Ivan,

    I am working with my own type which is based on generic content some of these items will have a parent and some wont.  I want the ones without a parent to show in the listing page.  I tried the code you have provided and the same thing occurs.

    below is the stack trace;
     Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx). 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
     
    Exception Details: System.FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx). 
     
    Source Error: 
     
    Line 47:            ContentFilterBuilder filter = new ContentFilterBuilder(this); 
    Line 48:            filter.AddFilter(new ContentFilterStatement("ParentID", ContentFilter.Condition.Null)); 
    Line 49:            return base.CreateDataSource();  
    Line 50:        }  
    Line 51:  
     
     
    Source File: D:\dev\******\******.CaseStudy\WebControls\PublicControl.cs    Line: 49 
     
    Stack Trace: 
     
    [FormatException: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).] 
       System.Guid..ctor(String g) +2486 
       Telerik.Cms.Engine.ContentViewFiltering.ContentFilterBuilder.ParseParentsFilter() +386 
       Telerik.Cms.Engine.ContentViewFiltering.ContentFilterBuilder.get_IsFilterValid() +48 
       Telerik.Cms.Engine.WebControls.ContentBasic.CreateDataSource() +86 
       ******.CaseStudy.WebControls.PublicControl.CreateDataSource() in D:\dev\******\******.CaseStudy\WebControls\PublicControl.cs:49 
       Telerik.Cms.Engine.WebControls.ContentView.CreateContentList() +502 
       Telerik.Cms.Engine.WebControls.ContentView.CreateChildControls() +5556 
       ******.CaseStudy.WebControls.PublicControl.CreateChildControls() in D:\dev\******\******.CaseStudy\WebControls\PublicControl.cs:73 
       System.Web.UI.Control.EnsureChildControls() +87 
       System.Web.UI.Control.PreRenderRecursiveInternal() +44 
       System.Web.UI.Control.PreRenderRecursiveInternal() +171 
       System.Web.UI.Control.PreRenderRecursiveInternal() +171 
       System.Web.UI.Control.PreRenderRecursiveInternal() +171 
       System.Web.UI.Control.PreRenderRecursiveInternal() +171 
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842 
     
     

  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    29 Sep 2009
    Link to this post
    Hi Kyle,

    The code works with the standard implementation for content items and their hierarchy. It will be great if you could share some of your custom logic, so we could try to figure out what is going wrong. Meanwhile you could attach your code to Visual Studio debugger and see whether a GUID is returned. The error states that the returned value is not a valid guid.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Kyle
    Kyle avatar
    6 posts
    Registered:
    18 Jun 2009
    29 Sep 2009
    Link to this post
    Hi There,

    I am not doing anything much different to the sample products implementation.  I have only started to customise it a little and this is one of the first problems I have run into, that I am unable to solve.  As you will see by looking at the stack trace the exception is thrown here Telerik.Cms.Engine.ContentViewFiltering.ContentFilterBuilder.ParseParentsFilter() before any items are returned.  And in the telerik code I used reflector and rebuilt the ContentFilterBuilder class I found that what is happening is the error is the ParseInternal method of Content Filter Builder returns the statement as 'ParentID Equal Null' if you look below you will see that this leads it to attempt to add new Guid(null) to a list of guids on line 17, which causes my error.

     I also note that if it had managed to reach the statement for null it would have added Guid.Empty to the list of guids
    of parent ids (therefore we should be able to achieve the same effect by adding an equal statement with Guid.Empty), however this produces an incorrect sql statement as it will attempt to retrieve items that have an id equal to {00000000-0000-0000-0000-000000000000} which is not the same as null.

    Are there any suggested methods for me to get around this? ATM it seems like the only option left to me is to retrieve all content items and filter them at the code level?  I am using sitefinity 3.7 without sp1 so I am downloading that in the hope that this problem is fixed.

    1 public virtual Guid[] ParseParentsFilter() 
    2
    3     List<Guid> list = new List<Guid>(); 
    4     foreach (IContentFilterStatement statement in this.ParseInternal(true)) 
    5     { 
    6         string str; 
    7         string[] strArray2; 
    8         int num; 
    9         if (!(statement.LeftOperand == "ParentID")) 
    10         { 
    11             continue; 
    12         } 
    13         switch (statement.Condition) 
    14         { 
    15             case ContentFilter.Condition.Equal: 
    16             { 
    17                 list.Add(new Guid(statement.RightOperand)); 
    18                 continue; 
    19             } 
    20             case ContentFilter.Condition.NotEqual: 
    21             case ContentFilter.Condition.GreaterThan: 
    22             case ContentFilter.Condition.LessThan: 
    23             case ContentFilter.Condition.GreaterOrEqual: 
    24             case ContentFilter.Condition.LessOrEqual: 
    25             case ContentFilter.Condition.Like: 
    26             case ContentFilter.Condition.NotNull: 
    27                 throw new NotSupportedException(); 
    28  
    29             case ContentFilter.Condition.In: 
    30                 strArray2 = statement.GetRightOperandWithoutSyntax().Split(new char[] { ',' }); 
    31                 num = 0
    32                 goto Label_00CF; 
    33  
    34             case ContentFilter.Condition.Null: 
    35             { 
    36                 list.Add(Guid.Empty); 
    37                 continue; 
    38             } 
    39             default: 
    40                 throw new ArgumentOutOfRangeException(); 
    41         } 
    42     Label_00B5: 
    43         str = strArray2[num]; 
    44         list.Add(new Guid(str)); 
    45         num++; 
    46     Label_00CF: 
    47         if (num < strArray2.Length
    48         { 
    49             goto Label_00B5; 
    50         } 
    51     } 
    52     return list.ToArray(); 
    53
    54  
    55  


    Regards,
    Kyle
  6. Kyle
    Kyle avatar
    6 posts
    Registered:
    18 Jun 2009
    29 Sep 2009
    Link to this post
    Hi There,

    Ah it seems that the 3.7 service pack has fixed the problem which caused it to crash.  However as you said in the original reply my control is showing nothing.  I believe this to be incorrect?  The system still translates the parent condition is null to guid.empty as a parent this lead to the following sql statement
    exec sp_executesql N'SELECT  TOP 10 ''DContentVersion;Application;ID;LangID;StatusLoc;DateModifiedLoc;DateCreatedLoc;OwnerLoc;'' AS NN_TABLE_ID_1, [dbo].[sf_CmsContentVersion].[Application], [dbo].[sf_CmsContentVersion].[ID], [dbo].[sf_CmsContentVersion].[LangID], [dbo].[sf_CmsContentVersion].[StatusLoc], [dbo].[sf_CmsContentVersion].[DateModifiedLoc], [dbo].[sf_CmsContentVersion].[DateCreatedLoc], [dbo].[sf_CmsContentVersion].[OwnerLoc] 
        , ''DCmsContentBase;Application;ID;MimeType;ParentID;Url;LoweredUrl;CommentsCount;ItemInfo;Creator;'' AS NN_TABLE_ID_2, [dbo].[sf_CmsContentBase].[Application], [dbo].[sf_CmsContentBase].[ID], [dbo].[sf_CmsContentBase].[MimeType], [dbo].[sf_CmsContentBase].[ParentID], [dbo].[sf_CmsContentBase].[Url], [dbo].[sf_CmsContentBase].[LoweredUrl], [dbo].[sf_CmsContentBase].[CommentsCount], [dbo].[sf_CmsContentBase].[ItemInfo], [dbo].[sf_CmsContentBase].[Creator] 
         
        FROM [dbo].[sf_CmsContentVersion] LEFT OUTER JOIN [dbo].[sf_CmsContentBase] ON ([dbo].[sf_CmsContentVersion].[Application] = [dbo].[sf_CmsContentBase].[Application] AND [dbo].[sf_CmsContentVersion].[ID] = [dbo].[sf_CmsContentBase].[ID])  WHERE ( ([dbo].[sf_CmsContentVersion].[Application] = @P_p1 AND [dbo].[sf_CmsContentVersion].[LangID] = @P_p2 AND [dbo].[sf_CmsContentVersion].[StatusLoc] = @P_p3 AND [dbo].[sf_CmsContentBase].[ParentID] = @P_p4 ) )  ORDER BY DateModifiedLoc DESC',N'@P_p1 nvarchar(10),@P_p2 int,@P_p3 int,@P_p4 uniqueidentifier',@P_p1=N'/CaseStudy',@P_p2=127,@P_p3=1,@P_p4='00000000-0000-0000-0000-000000000000' 

    As you can see the parentid condition is 'AND [dbo].[sf_CmsContentBase].[ParentID] = @P_p4 ' which means it will only get items with a parent guid set to 00000000-0000-0000-0000-000000000000, which is not going to happen unless the parent is specifically set to 00000000-0000-0000-0000-000000000000, which by default it doesnt.  Is this the expected behaviour?  Is it suggested in this case that I set the parentId to Guid.Empty?

  7. Kyle
    Kyle avatar
    6 posts
    Registered:
    18 Jun 2009
    29 Sep 2009
    Link to this post
    Hi,

    I tried setting the parent id for a content item to Guid.Empty however this seems to get checked and turned into null at some point as it never reaches the database.  Choosing another random guid that doesnt exist seems to work so it isnt checking to make sure that the id exists first.
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    05 Oct 2009
    Link to this post
    Hi Kyle,

    The filter and our provider does not support required functionality - items with null value. When, there is no ParentID, the value in the database is NULL. When parsing the filter we call Guild.Empty which generate a string of 32 zeros. As a workaround you could write {00000000-0000-0000-0000-000000000000} as a value for all items that do not have ParentID. By doing so you can use the condition below:

    filter.AddFilter(new ContentFilterStatement("ParentID", ContentFilter.Condition.Null));  

    You can add some logic when your items are created so the database will be automatically updated.

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  9. Kyle
    Kyle avatar
    6 posts
    Registered:
    18 Jun 2009
    05 Oct 2009
    Link to this post
    Hi Ivan,

    thanks for the reply.  As per my previous reply I found that setting the ParentId = Guid.Empty when creating a content item doesnt make it to the database, it will instead save with the ParentId  value set to null.  I could create a trigger to sort this out but i am unsure how deep into the data saving code I can get for generic content?  The fastest solution (its also pretty ugly) I found was to choose a random guid to represent null and code that into the application...
  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    05 Oct 2009
    Link to this post
    Hello Kyle,

    The Nolics tTransaction converts the Guid.Empty to null. The only way is manually changing the transaction or updating the ParentID column for all items where ParentId is "NULL". The converting comes from Nolics, generally we set Guid.Empty as initial value.

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Register for webinar
10 posts, 0 answered