want to alter the posts lists in admin/posts.aspx

Topics: Business Logic Layer
Jan 16, 2011 at 8:28 AM

Looks like you have something automatically inserting that table into the div id=container

can you tell me where that code is so i can add an additional column?

thanks

Coordinator
Jan 17, 2011 at 7:17 AM

It's "posts.htm" in the /Templates folder.

Jan 17, 2011 at 6:44 PM

Thanks. I don't have the code in front of me right now so hopefully i'll figure this one out on my own later tonight, but if not, which file do i edit as far as being able to pull that extra column from the database that i want to display in the admin/posts.aspx table?

thanks

Jan 18, 2011 at 6:01 AM
Edited Jan 18, 2011 at 6:05 AM

hmm - maybe i bit off more then i can chew with this. I seem to be endlessly digging through a pile of functions/.cs pages and i still haven't found what it is I need to change in order to get an additional column from the posts table to show up in the admin/posts.aspx page. I've gotten as far as dbblogprovider.cs and past that i'm basically lost. I've found FillPosts, which seems to get a list of Guid's and then  ... not really sure what happens here:
var posts = postIDs.Select(id => Post.Load(new Guid(id))).ToList(); 

Do i edit jsonposts.cs and alter the getpost function?

 

                var jp = new JsonPost
                {
                    Id = x.Id,
                    Author = GetAuthor(x.Author),
                    Title = string.Format("<a href=\"{0}\">{1}</a>", x.RelativeLink, x.Title),
                    Date = x.DateCreated.ToString("dd MMM yyyy"),
                    Time = x.DateCreated.ToString("t"),
                    Categories = GetCategories(x.Categories),
                    Tags = GetTags(x.Tags),
                    Comments = GetComments(x.Comments, x.RelativeLink),
                    IsPublished = x.IsPublished,
                    CanUserEdit = x.CanUserEdit,
                    CanUserDelete = x.CanUserDelete
                };

 

Would i insert a line there like MyCoulm = x.MyColumn.ToString()

And looks like i'd have to edit posts.cs and add in my column into public static List<Post> Posts

for what i'm trying to do, i've just added an autonumber int field to the posts table and i want to display this number in the admin/posts.aspx table. And i need to be able to reference that property on other pages. I;m guessing if i add that to the post properties in post.cs i could just reference it as Post.Mycolumn. Right now though i just seem to be going in circles. Sorry to be such a bother, but i hope you can point me in the right direction ... again.

thanks ... again

Coordinator
Jan 18, 2011 at 7:30 AM

Looks like you've touched on all the major parts of the code.  At first, there's a lot to digest ...

You have a new column/property for a post.  Following the conventions/pattern that's there now, you would want to add the following to Post.cs (in the BE core).

1.  Add a new private field (myColumn, type integer).

private int myColumn;

2.  Add a property for this.

public int MyColumn
{
	get
	{
		return this.myColumn;
	}

	set
	{
		base.SetValue("MyColumn", value, ref this.myColumn);
	}
}

3.  In the DbBlogProvider, there's 3 places to edit -- SelectPost, InsertPost, and UpdatePost.

In SelectPost(), you can add MyColumn to the end of the list of columns being selected (line break inserted and cut out some of the columns).

var sqlQuery = string.Format("SELECT PostID, Title, ... IsDeleted, MyColumn
FROM {0}Posts WHERE PostID = {1}id", this.tablePrefix, this.parmPrefix);

And you need to assign the MyColumn value from the reader a little lower down where all the other properties are being assigned.

if (!rdr.IsDBNull(13))
{
	post.MyColumn = rdr.GetInt32(13);
}

Actually, in UpdatePost and InsertPost, you may not need to add MyColumn if it's an autonumber column that is generated in the DB.  So updating UpdatePost should not be necessary.

But you probably need to get the autonumber value out of the DB, because it won't be in the Post object.  And SelectPost is only called when the blog starts up.  So in InsertPost (still in DbBlogProvider.cs), towards the bottom, after everything has been inserted, you can call SelectPost to get the MyColumn value and assign that to the "post".  So towards the bottom of InsertPost, you would want to add the part shown below as "NEW code" (after this.UpdateNotify....).

........
// Email Notification
this.UpdateNotify(post, conn);

..... NEW code START .....
Post postFromDb = SelectPost(post.Id); post.MyColumn = postFromDb.MyColumn; ..... NEW code END .....

4.  In JsonPost.cs, you will want to add a new property for MyColumn:

public int MyColumn { get; set; }

5.  Finally, you can add MyColumn to that block of code in JsonPosts.cs you pasted here before (I added MyColumn at the end).

var jp = new JsonPost
{
	Id = x.Id,
	Author = GetAuthor(x.Author),
	Title = string.Format("<a href=\"{0}\">{1}</a>", x.RelativeLink, x.Title),
	Date = x.DateCreated.ToString("dd MMM yyyy"),
	Time = x.DateCreated.ToString("t"),
	Categories = GetCategories(x.Categories),
	Tags = GetTags(x.Tags),
	Comments = GetComments(x.Comments, x.RelativeLink),
	IsPublished = x.IsPublished,
	CanUserEdit = x.CanUserEdit,
	CanUserDelete = x.CanUserDelete,
	MyColumn = x.MyColumn
};

...... this all assumes again that MyColumn is populated automatically on the DB side since it is an autonumber or identity column.

I would test it out too by creating a new post.  If it works, I would restart the blog (make any change to the web.config file, etc), and make sure when the data is re-read from the DB again, that MyColumn is correctly read from the DB.  You can probably verify this on the admin Posts page where you are outputting MyColumn to make sure the value exists, and is not 0 (zero).

Jan 18, 2011 at 7:51 AM

just about to head to bed now, but i just wanted to thank you for taking the time to explain all that. I really appreciate it. I'll give that a go tomorrow.

And yeah - the int value is generated in the DB. Would i need to create the 'set' for the property since it's only set by the db and never changed by the user?

and thank you again for the help! I already donated once to you - but i think after all this i'm going to feel guilty if i dont donate again considering how much of your time i've taken. :)

Coordinator
Jan 19, 2011 at 5:34 AM

You do need the "set" for MyColumn, because MyColumn gets a value set to it in SelectPost, as well as in InsertPost (based on that code I posted).

Jan 22, 2011 at 5:04 PM
Edited Jan 24, 2011 at 3:03 PM

never mind. fixed my issue.

Thanks!