Displaying List Item Content [Blog Posts] Programatically Using ASP
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:
|
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.
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