ASPX templates
Version 5.x > ASPX templates > Update a Document type progrmatically View modes: 
User avatar
Member
Member
hemanthray-gmail - 1/24/2011 10:53:19 AM
   
Update a Document type progrmatically
Well we have events where we upload them Programatically. I dint face any issue with Inserting them but I face issue while updating them through code. Can I please get assistance regarding that thanks.

Here is the Code which I have . "2011 Meeting" is a event created under Folder Events but it doenst update it creates a new one can you please let me know what is the issue with the code

#region "Adding Events to Kentico"
for (i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string newsName = ds.Tables[0].Rows["Meeting_Name"].ToString();

// create tree provider instance

CMS.TreeEngine.TreeProvider provider = new CMS.TreeEngine.TreeProvider(CMS.CMSHelper.CMSContext.CurrentUser);

// get parent node for new document
CMS.TreeEngine.TreeNode parent = provider.SelectSingleNode(CMS.CMSHelper.CMSContext.CurrentSiteName, "/Events", "en-us");
// create a new tree node
CMS.TreeEngine.TreeNode node = new TreeNode("CMS.BookingEvent", provider);
CMS.TreeEngine.TreeNode Childnode = provider.SelectSingleNode(CMS.CMSHelper.CMSContext.CurrentSiteName, "/Events/2011 Meeting", "en-us");


if (Childnode != null)
{
// Update values
node.SetValue("EventName", newsName);
node.SetValue("EventDetails", ds.Tables[0].Rows["Description"].ToString() + "<br/>" + ds.Tables[0].Rows["RegistrationLink"].ToString());
// Update the document
Childnode.Update();
}
else
{
if (parent != null)
{
// set document properties
node.NodeName = ds.Tables[0].Rows["Meeting_Name"].ToString();
node.NodeAlias = ds.Tables[0].Rows["Meeting_Name"].ToString();
node.SetValue("DocumentCulture", "en-us");
node.SetValue("EventName", newsName);
node.SetValue("EventSummary", ds.Tables[0].Rows["Description"].ToString());
node.SetValue("EventDetails", ds.Tables[0].Rows["Description"].ToString());
node.SetValue("EventLocation", ds.Tables[0].Rows["Location"].ToString());
node.SetValue("EventDate", Convert.ToDateTime(ds.Tables[0].Rows["Start_date"].ToString()));
node.SetValue("EventOpenFrom", Convert.ToDateTime(ds.Tables[0].Rows["Start_date"].ToString()));
node.SetValue("EventOpenTo", Convert.ToDateTime(ds.Tables[0].Rows
["Start_date"].ToString()));

// create New document
node.Insert(parent.NodeID);
}
}

}

Thanks
hemanth

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 1/26/2011 2:32:05 AM
   
RE:Update a Document type progrmatically
Hi,

to share the solution, here is an extract from email communication with support.

the SelectSingleNode method requires alias path as a second parameter in the following line of code.


CMS.TreeEngine.TreeNode Childnode =
provider.SelectSingleNode(CMS.CMSHelper.CMSContext.CurrentSiteName, "/Events/2011 Meeting", "en-us");


It means that in the "/Events/2011 Meeting" should not be space. With the above code the Childnode is never found -> is always null -> the update code is never reached.

Best regards,
Ivana Tomanickova


User avatar
Member
Member
sheeba.chandran-lobaansoftwares - 9/12/2013 2:33:09 AM
   
RE:Update a Document type progrmatically
Hello...

TreeProvider provider = new TreeProvider(CMSContext.CurrentUser);
CMS.TreeEngine.TreeNode parent = provider.SelectSingleNode(CMSContext.CurrentSiteName, "/Company-Partners", "en-us", true);

can you plz explain me why i am getting parent a null value?
And what is the use of second parameter "/Company-Partners", n how to decide what should be the value for this second parameter

User avatar
Kentico Support
Kentico Support
kentico_filipl - 9/12/2013 3:11:47 AM
   
RE:Update a Document type progrmatically
Hello,

Parameters for SelectSingleNode method are (String siteName, String aliasPath, String cultureCode, Boolean combineWithDefaultCulture) in this case. AliasPath is the path to the document (which is a TreeNode object) you want to retrieve. So if you do not have such a document under "/Company/Partners" path (you can check it in CMS Desk -> Content -> Content management), the method returns null.

A complete list of all API methods can be found in API Reference.

Best regards,
Filip Ligac