More in this section

Forums / Developing with Sitefinity / Displaying List Item Content [Blog Posts] Programatically Using ASP

Displaying List Item Content [Blog Posts] Programatically Using ASP

2 posts, 1 answered
  1. MBA
    MBA avatar
    11 posts
    Registered:
    08 Jul 2008
    12 Sep 2011
    Link to this post
    Hi All,

    After a mammoth session learning (and subsequently patching) SiteFinity 4.2, the last hurdle I need to overcome is the ability to get the content of a List Item and assign it to an ASP (C#) variable.

    Essentially I'd like to be able to loop over all given List Items (in this case, blog posts) and assign the content of the ListItem (Blog Post) to a C# variable which I can then manipulate.

    Does anyone have any experience doing this? I've followed an old (SF3.x, Visual Basic) how-to and managed (I think!) to translate it to SF4.2 and C#. The code appears below.

    Unfortunately, I cannot get the code to load anything into the list. Visiting the page in a browser gives the following error (ASP gives Line 6, below, as the point of occurance):

    I have a feeling I might need to use a Linq Query to extract the posts I want, however I'm not so sure how to go about it. Can anyone give me a pointer or helping hand please?


    Exception Details: Telerik.Sitefinity.SitefinityExceptions.ItemNotFoundException: You are trying to access item that no longer exists. The most probable reason is that it has been deleted by another user.

    Stack Trace: 

    [ItemNotFoundException: You are trying to access item that no longer exists. The most probable reason is that it has been deleted by another user.]
       DynamicModule.ns.Wrapped_OpenAccessListsDataProvider_e1f3a1b538c2464d870ac9deb2d5665e.GetList(Guid id) +253
       Telerik.Sitefinity.Modules.Lists.ListsManager.GetList(Guid id) +45
       SitefinityWebApp.Controls.TestBlog.Page_Load(Object sender, EventArgs e) in C:\inetpub\wwwroot\pp\Controls\TestBlog.aspx.cs:23
       System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
       System.Web.UI.Control.OnLoad(EventArgs e) +91
       System.Web.UI.Control.LoadRecursive() +74
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207




    01.protected void Page_Load(object sender, EventArgs e)
    02.        {
    03.            /* Setup array to hold posts */
    04.            string[] strContent = null;
    05. 
    06.            /* Define new list manager for access to lists */
    07.            Telerik.Sitefinity.Modules.Lists.ListsManager mgrList = new Telerik.Sitefinity.Modules.Lists.ListsManager();
    08. 
    09.            /* Define GUID of list to load */
    10.            Guid searchForGuid = new Guid("854FD531-2B15-4277-B616-49A36D7FBB85");
    11. 
    12.            /* Load list into new SiteFinity list */
    13.            Telerik.Sitefinity.Lists.Model.List objLists = mgrList.GetList(searchForGuid);
    14. 
    15.            /* Define a list item for use later */
    16.            Telerik.Sitefinity.Lists.Model.ListItem thisItem = new Telerik.Sitefinity.Lists.Model.ListItem();
    17. 
    18.            /* Load list into more system friendly model */
    19.            System.Collections.IList sysList = objLists.ListItems.ToList();
    20. 
    21.            /* Loop over system friendly list and write content to array */
    22.            foreach ( int r in sysList)
    23.            {
    24.                int c = 0;
    25.                /* Assign content to array */
    26.                strContent[c]=thisItem.Content;
    27. 
    28.                /* Output something to the screen */
    29.                Response.Write("Loop: " + c.ToString() + "<br />");
    30.                Response.Write(strContent[c]);
    31.                c++;
    32.            }
    33.        }

  2. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    18 Sep 2017
    15 Sep 2011
    Link to this post
    Hello Jeff,

    Here is the API code that will get the contents of the list item.

    String myListName = "List";
                ListsManager manager = ListsManager.GetManager();
                Telerik.Sitefinity.Lists.Model.ListItem currentItem = manager.GetListItems()
                    .Where(i => i.Parent.Title == myListName)
                    .FirstOrDefault();
                if (currentItem != null)
                {
                    var content = currentItem.GetValue("Content");
    //Content is the name of the field in list that represents the editor where you enter contents
                }
    You can review all field names by going to Lists module entering a list and in the right sidebar clicking custom fields for lists. There is a tab with all the default field names.

    Greetings,
    Stanislav Velikov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
2 posts, 1 answered