Use additional columns in custom extension

Topics: Business Logic Layer, Controls
Jul 18, 2011 at 5:39 PM

Hi there,

I would like to store some additional info about a post like latitude and longitude so I can show it on a map. What is the best way of doing this:

- adding extra columns to the database

- use those columns to display a bing maps control

Should I write my own custom extension for it? Or do I need to do more and what about deployment if I want to give it to the public?

 

Coordinator
Jul 18, 2011 at 7:55 PM

You could "save" things like latitude/longitude as part of the post using convention, for example [lat:123;long:456] and then custom extension can take values inside brackets and replace whole thing with map centered on that location.

If all you want is just add a map, Windows Live Writer that has several map plugins including google's.

Jul 18, 2011 at 8:37 PM

Basically I want to accomplish this:

- I want to store gps information together with the post (I will use a mobile device to get the gps signal and attacht it to a post, probably using a web service or something)

- When viewing a post one should see a little map, for example using google

- There willl be a seperate page/control that will display all gps coordinates of blog post in a certain category on a map

- Would be super if it is possible to show a little map when creating a new blog post to select the gps coordinates for that post

So i can have a good blog for travelling. Each blog post will go in a different category, like "Switzerland 2011". I did not found any blog that is capable of doing that, and since you want to have the best blogging engine on earth we have to build it ;-)

You can then have a mixup between a blog and a mapping site like this: http://www.a-trip.com/users/home/14611

Dec 22, 2011 at 11:58 PM
Edited Dec 23, 2011 at 3:17 PM
rtur wrote:

You could "save" things like latitude/longitude as part of the post using convention, for example [lat:123;long:456] and then custom extension can take values inside brackets and replace whole thing with map centered on that location.

If all you want is just add a map, Windows Live Writer that has several map plugins including google's.

Hi rtur!

I did this and got it to work. But after 4 posts, I realized it would be asking the editors too much coding because of the html that displays when a viewer clicks on the google map pin.

Is there any way to use an external table (xml in my case) to store the lat/long and html info? Its primary key would be the post id. I already know how to extend the Admin Control Panel to host the extended table. I got a new tab ready to display my extended table. Just don't know how to access the external table either in the Admin Control Panel nor in the Post.

Thanks!

Dec 23, 2011 at 3:42 PM

Galagerardo,

Check out this post:

How to Add a Custom Field to a Post in BlogEngine.Net

 

You will then have a new field in the Post Add_entry.aspx page

Your editors just input the cords into that text box and thats it.

 

Dec 23, 2011 at 4:24 PM
Edited Dec 23, 2011 at 4:35 PM

Hi kbdavis07,

I was hoping for a way to add new tables in XML without having to modify the core. Just add a new Folder with a specific name in a specific location with files with specific names. This way my blog is always ready for a new version. I don't have to take the new version and then modify it and re-compile.

I'm looking for a true plug-and-play solution. I've seen this done before just don't know how to make it work in the blogengine.

I've also seen  systems where the tables have custom colums pre-built and you just pick and choose which custom columns you want to use and then add your own code (outside of core) to access those columns.

Thanks for your input.

Coordinator
Dec 23, 2011 at 4:33 PM

Not completely sure on your usage scenario, but this post shows how to create custom admin for extension that uses a table with data saved as extension settings.

Dec 23, 2011 at 5:10 PM

Its possible :)

 

Look at this source code:

Posts.cs

Look this part:

 

public override List<Post> FillPosts()
        {
            var folder = this.Folder + "posts" + Path.DirectorySeparatorChar;
            var posts = (from file in Directory.GetFiles(folder, "*.xml", SearchOption.TopDirectoryOnly)
                         select new FileInfo(file)
                         into info
                         select info.Name.Replace(".xml", string.Empty)
                         into id
                         select Post.Load(new Guid(id))).ToList();

            posts.Sort();
            return posts;
        }

        /// <summary>
        /// Inserts a new Post to the data store.
        /// </summary>
        /// <param name="post">
        /// The post to insert.
        /// </param>
        public override void InsertPost(Post post)
        {
            if (!Directory.Exists(string.Format("{0}posts", this.Folder)))
            {
                Directory.CreateDirectory(string.Format("{0}posts", this.Folder));
            }

            var fileName = string.Format("{0}posts{1}{2}.xml", this.Folder, Path.DirectorySeparatorChar, post.Id);
            var settings = new XmlWriterSettings { Indent = true };

            var ms = new MemoryStream();

            using (var writer = XmlWriter.Create(ms, settings))
            {
                writer.WriteStartDocument(true);
                writer.WriteStartElement("post");

                writer.WriteElementString("author", post.Author);
                writer.WriteElementString("title", post.Title);
          

All you do is just change the folder and file name to what you want to use.

 

Then just modify the code to what you need thats it.

 

 

But, the question now is how to you want to pull in this data through?

 

You can modify the Add_entry.aspx.cs  to add the field and modify the data for your cords text box from

your cords folder and file.

 

So from your Add Entry page you will have a new field "Cords"

Editor enter the info in the "Cords" box.  Just like they do for "keywords" and "title" and etc. No "coding" on their side.

That data is then inserted into your [cords.xml] file.

Then when you need that data just query your [cords.xml] file.

 

The disadvantages of doing it this way is that you don't get to use the built-in BlogEngine Features through.

You will have to query the file each time you need it and its not in the "app_pool"

If you have it in the app_pool you can use LINQ with In memory objects to use your data like this:

posts.cords

 

Like tags for example below:

 

var tags = new string[post.Tags.Count];
                for (var i = 0; i < post.Tags.Count; i++)
                {
                    tags[i] = post.Tags[i];
                }

                txtTags.Text = string.Join(",", tags);
            }

You don't have to load the file to get the Tags because its in memory in the app_pool.

Other wise before you can get the tags you would first have to load the xml file that has the tags in them.

 

You do bring up a good point through about being truly , "Plug and Play".

I tried loading xml directly from my own file for the PageLinkMenu but since it used too many Core features of BE it didn't make sense to do so,

so I had to change the core.

 

But, for using features that are not core elements and you only have 1 element to add instead of 4 which I had for PageLinkMenu it should be ok

to just have a plug and play.

 

This will be another item to my long "To-Do-List".   To figure out a way to have a "Plug and Play" feature for custom fields in BE either in Post or Pages or anything else.

 

Need a way to insert "Plug and Play" xml files/folders into the "app_pool" from within the application out side of the core.

This way all you do is upload your xml folder or single file and thats it.

Its in the "app_pool" and can use in memory resources to use the data in the files with out having to load the file then query it every time.

 

Well hope this helps, and keep us posted on your progress on this.

 

Thanks,

 

Brian Davis

Dec 23, 2011 at 5:55 PM

Or you can use the Extension manager like what Rtur posted

Dec 23, 2011 at 7:08 PM
Edited Dec 24, 2011 at 1:12 AM

Hi!

I downloaded and installed rtur's extension.

One minor bug (or I missed the instructions) - I had to modify the web.sitemap so a new tab shows up in the Admin Control Panel.

I'm making progress full speed and I'm just jolly for the holidays!

Thanks!

Coordinator
Dec 24, 2011 at 1:00 AM

You shouldn't modify anything, admin pages for any  extension are under "extensions" tab, in the right side-bar menu.

Dec 24, 2011 at 1:24 AM
rtur wrote:

You shouldn't modify anything, admin pages for any  extension are under "extensions" tab, in the right side-bar menu.


It might be because I'm still on 2.0.0.36 which might also explain why I had to use Utils.RelativeWebRoot instead of Blog.CurrentInstance.RelativeWebRoot. Other than this, it's working fine - I can see the NivoSlider on my Post.

Now that I have this working, I can use it as a pattern for modifying my own Extension for storing the Long/Lat of a Post and I will have an Admin Control Panel tab for editing the values of Long/Lat of a Post. It will be elegant for the Users and yet I don't have to modify the Core.

Thanks!

Dec 24, 2011 at 8:33 PM
Edited Dec 25, 2011 at 7:56 AM
Expecho wrote:

Basically I want to accomplish this:

- I want to store gps information together with the post (I will use a mobile device to get the gps signal and attacht it to a post, probably using a web service or something)

- When viewing a post one should see a little map, for example using google

- There willl be a seperate page/control that will display all gps coordinates of blog post in a certain category on a map

- Would be super if it is possible to show a little map when creating a new blog post to select the gps coordinates for that post

So i can have a good blog for travelling. Each blog post will go in a different category, like "Switzerland 2011". I did not found any blog that is capable of doing that, and since you want to have the best blogging engine on earth we have to build it ;-)

You can then have a mixup between a blog and a mapping site like this: http://www.a-trip.com/users/home/14611

I got mine to work just like yours! Yehey!

Jan 5, 2012 at 3:20 PM
Edited Jan 5, 2012 at 3:24 PM
rtur wrote:

Not completely sure on your usage scenario, but this post shows how to create custom admin for extension that uses a table with data saved as extension settings.

Hi rtur!

I got my extension to work just like your but after a while I realized that your extension only has a "Delete" link on the recordset rows. How do I put a "Tools" button that allows edit and delete functionality just like the Post Admin Panel. I looked at Post.aspx.cs and there is no code in it. The only thing I notice is that the pages that have the "Tools" button are using the "Pager" class. But your code is building the table manually.

Did you build your table by code because the Extensions don't have the dynamic data capability?

I can see that I need to add an EditPage.ascx and a Menu.ascx if I were to follow the Page Admin Panel. But why didn't you do it that way? Am I missing anything?

Thanks!

Coordinator
Jan 5, 2012 at 3:55 PM

What you really have to look at is a Templates/posts.htm - this is a code that replaces "content" div by jquery templating engine when posts.aspx is loaded. And even better look at Tags.aspx - posts simply redirect to edit post page, tags.aspx uses in-place editing with ajax call and raw manipulation using jquery.

Jan 5, 2012 at 9:42 PM

Ok rtur, now I see why you went your way. The other way meant modifying the core. And I'm avoiding that.

So I added a new column for edit similar to delete column. The user clicks on the link and then I take him to the Add/Edit page.

BUT! I don't see any code for editing the ImageData. I know the Index but now what?

Thanks! I'm almost there. Just one more nudge please.

Jan 6, 2012 at 5:27 PM

Hi rtur!

I couldn't get the update to work. So I did a delete and an insert. Seems to be working for now.

Thanks!