I am using umbraco 4.7.0. how can I insert records in another DB from umbraco's admin panel.
say, I want that when user creates a new content page, I want that user can select books from the list related to this page. books names from another DB table. on save/publish of this content page I want to insert nodeid of this page, bookid of selected books into a new table say book_node_mapping. so is that possible?
can I have detailed steps for the same? I am new to umbraco.
so can you people guide me further. suppose I use usercontrol wrapper, then depending on user selection of book and when the page is saved, can I update outside DB? I mean can I handle this update in usercontrol itself?
I'm not completely sure if I understand what you are trying to do so the assumptions I am making are:
Your list of books come from another database
When a document is saved you want the id of the document saved against this book in the other database
First step I would create a user control or some custom data type that shows the books. This will reference a connection string in the webconfig (you can have more than one) to get the list of books. When the document is saved this will store the id or name or what ever you tell it to in the umbraco xml for that page..
(assuming you know how to do this part)
Also on the save you want it to save to the other database;
to do this I would overrride the onsave or on publish of the document
A list of all events you can override can be found here.
Here is an example of overriding an event, "before publish" in this case but it should give you the idea.
Bare in mind this event will fire on every "before publish" of any document so you need to check the document type of the document it's firing on, then grab the id of the book by looking at the properties of the document and then do what ever you need with it.
There maybe a way of doing it with a custom datatype but this is the easiest way I can think of!
Check out this video, think it should explain how to create a datatype from a user control. Not done it for a while, but as far as I remember you just return a value, eitherway umbraco will save it.
It's very simple.
If you need something more complicated you can create a custom datatype with c# classes, ut can't find a good example of that!
I got the concept of custom data type, tried it. Also saw events and tried it. I am able to modify DB.
I still need guidance exactly for my requirement. here it is....
Say I create custom datatype using user control wrapper (say List, which allows multiple selection.). Using that on one of the template. Now on before_publish event I want to access this list and want to insert rows in another DB for the selected values of list.
Can you give some code of how far you have got already and I may be able to point you in the right direction. You have said you have looked at events and you are able to modifiy the database, what is the exact bit you are struggling with?
using System; using System.Collections.Generic; using System.Linq; using System.Web; using umbraco.BusinessLogic; using umbraco.cms.businesslogic; using umbraco.cms.businesslogic.web; using UserDatatypes;
namespace UmbracoEvents { public class AppBase : umbraco.BusinessLogic.ApplicationBase {
public AppBase() { Document.BeforePublish += new Document.PublishEventHandler(Document_BeforePublish); }
Log.Add(LogTypes.Debug, sender.Id, "the document " + sender.Text + " is about to be published");
if (sender.ContentType.Alias == "BooksPage") {
book b = new book(); b.insertBookNode(sender.getProperty("listOfBooks").Value.ToString());
sender.Save(); }
//cancel the publishing //e.Cancel = true; } } }
-- UserDatatypes namespace is having code of user control wrapper ... so .ascx.cs file is having following code....
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Data.Common; using umbraco.BusinessLogic; using umbraco.cms.businesslogic; using umbraco.cms.businesslogic.web;
namespace UserDatatypes { public partial class BookList : System.Web.UI.UserControl, umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack || DropDownList1.SelectedValue.Length.ToString() == "0") { fillBooks(DropDownList1);
} }
private void fillBooks(DropDownList DropDownList1) { book b = new book(); string st = "select book_id, book_name from book_master";
DataTable dt = b.fetchdata(st, null); // dynamically fill book list in dropdownlist if (dt != null) { DropDownList1.Items.Clear(); DropDownList1.DataValueField = "BOOK_ID"; DropDownList1.DataTextField = "BOOK_NAME"; DropDownList1.DataSource = dt; DropDownList1.DataBind(); ListItem l1 = new ListItem(); l1.Text = "All"; l1.Value = "0"; DropDownList1.Items.Insert(0, l1);
if (!string.IsNullOrEmpty(_umbval)) { DropDownList1.SelectedValue = _umbval; } } }
#region IUsercontrolDataEditor Members
private string _umbval; public object value { get {
return DropDownList1.SelectedValue;
//throw new NotImplementedException(); } set { _umbval = value.ToString(); //throw new NotImplementedException(); } }
#endregion
}
}
in this user control I have one dropdown having books names.... so on before publish I can access the selected value with b.insertBookNode(sender.getProperty("listOfBooks").Value.ToString()); in before_publish.
.... now say instead of dropdown I want to access Listbox in which user can select multiple books, so how can I pass its value and update that many rows in another DB...? I am stuck here now.
accessing another DB from admin panel
I am using umbraco 4.7.0. how can I insert records in another DB from umbraco's admin panel.
say, I want that when user creates a new content page, I want that user can select books from the list related to this page. books names from another DB table. on save/publish of this content page I want to insert nodeid of this page, bookid of selected books into a new table say book_node_mapping. so is that possible?
can I have detailed steps for the same? I am new to umbraco.
anybody there to help? its a bit urgent
you can create a custom datatype that will add a dropdown of your books, there's a nice video explaing how to create a datatype
http://stream.umbraco.org/video/1533917/tim-geyssens-master-of
good luck :)
ok will see. and revert back
if i was you:
- create user control to fetch book name, book id .................
- use usercontrol wrapper, it provides you to use usercontrol as datatype.
Eran Meir,
the link of video u sent is playing upto 15 mins... not after that....
so can you people guide me further. suppose I use usercontrol wrapper, then depending on user selection of book and when the page is saved, can I update outside DB? I mean can I handle this update in usercontrol itself?
Hi!
Here's what I would do...
I'm not completely sure if I understand what you are trying to do so the assumptions I am making are:
This will reference a connection string in the webconfig (you can have more than one) to get the list of books.
When the document is saved this will store the id or name or what ever you tell it to in the umbraco xml for that page..
you have understood correct.
you wrote,
"When the document is saved this will store the id or name or what ever you tell it to in the umbraco xml for that page..
Check out this video, think it should explain how to create a datatype from a user control.
Not done it for a while, but as far as I remember you just return a value, eitherway umbraco will save it.
It's very simple.
If you need something more complicated you can create a custom datatype with c# classes, ut can't find a good example of that!
I got the concept of custom data type, tried it. Also saw events and tried it. I am able to modify DB.
I still need guidance exactly for my requirement. here it is....
Say I create custom datatype using user control wrapper (say List, which allows multiple selection.). Using that on one of the template. Now on before_publish event I want to access this list and want to insert rows in another DB for the selected values of list.
Can you give some code of how far you have got already and I may be able to point you in the right direction.
You have said you have looked at events and you are able to modifiy the database, what is the exact bit you are struggling with?
yes... here is the code written in .cs file ...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.web;
using UserDatatypes;
namespace UmbracoEvents
{
public class AppBase : umbraco.BusinessLogic.ApplicationBase
{
public AppBase()
{
Document.BeforePublish += new Document.PublishEventHandler(Document_BeforePublish);
}
void Document_BeforePublish(Document sender, PublishEventArgs e)
{
Log.Add(LogTypes.Debug, sender.Id, "the document " + sender.Text + " is about to be published");
if (sender.ContentType.Alias == "BooksPage")
{
book b = new book();
b.insertBookNode(sender.getProperty("listOfBooks").Value.ToString());
sender.Save();
}
//cancel the publishing
//e.Cancel = true;
}
}
}
-- UserDatatypes namespace is having code of user control wrapper ... so .ascx.cs file is having following code....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic;
using umbraco.cms.businesslogic.web;
namespace UserDatatypes
{
public partial class BookList : System.Web.UI.UserControl,
umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack || DropDownList1.SelectedValue.Length.ToString() == "0")
{
fillBooks(DropDownList1);
}
}
private void fillBooks(DropDownList DropDownList1)
{
book b = new book();
string st = "select book_id, book_name from book_master";
DataTable dt = b.fetchdata(st, null); // dynamically fill book list in dropdownlist
if (dt != null)
{
DropDownList1.Items.Clear();
DropDownList1.DataValueField = "BOOK_ID";
DropDownList1.DataTextField = "BOOK_NAME";
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
ListItem l1 = new ListItem();
l1.Text = "All";
l1.Value = "0";
DropDownList1.Items.Insert(0, l1);
if (!string.IsNullOrEmpty(_umbval))
{
DropDownList1.SelectedValue = _umbval;
}
}
}
#region IUsercontrolDataEditor Members
private string _umbval;
public object value
{
get
{
return DropDownList1.SelectedValue;
//throw new NotImplementedException();
}
set
{
_umbval = value.ToString();
//throw new NotImplementedException();
}
}
#endregion
}
}
in this user control I have one dropdown having books names.... so on before publish I can access the selected value with b.insertBookNode(sender.getProperty("listOfBooks").Value.ToString()); in before_publish.
.... now say instead of dropdown I want to access Listbox in which user can select multiple books, so how can I pass its value and update that many rows in another DB...? I am stuck here now.
hey anyone any idea ? how can I pass more than one value as list will have multiple selection.
if I am using multiple selection listbox I am getting following error.
No mapping exists from object type System.Web.UI.WebControls.ListBox to a known managed provider native type.
I am not getting how to get / set listbox object with multiple values.
is working on a reply...