Get ItemID after inserting record into page type

Pritam Gupta asked on November 13, 2017 06:22

Hello,

How can i Get ItemID after inserting record into page type and after that i have to pass that value into another function. So can anyone please help to resolve this issue.

Thanks.

Recent Answers


Juraj Ondrus answered on November 13, 2017 12:52

Hi,
Your description is very short to provide more detailed answer but it looks like you may want to use an event handler for your pages (document events) to get the value which was saved and performa additional logic you need.

0 votesVote for this answer Mark as a Correct answer

Pritam Gupta answered on November 13, 2017 13:36

Hello Juraj,

Actaully i created one page Type(Live Streaming Page) and now i am going to create a page on the basis of that page Type (Live Streaming Page) which i created.

Now when i creating a new page than that newly created Page's ID i need to pass on some another function to process. please see the below code which i wrriten using Global Event.

Code

using System; using System.Collections.Generic; using System.Linq; using System.Web; using CMS; using CMS.DocumentEngine; using CMS.DataEngine; using System.Data; using CMS.EventLog; using GemPackAPI;

// Registers the custom module into the system

[assembly: RegisterModule(typeof(FCMNodeCreation))] public class FCMNodeCreation : Module { public FCMNodeCreation() : base("CustomInit") { // // TODO: Add constructor logic here // }

// Contains initialization code that is executed when the application starts
protected override void OnInit()
{
    base.OnInit();

    // Assigns custom handlers to events
    DocumentEvents.Insert.After += Document_Insert_After;
    DocumentEvents.InsertLink.Before += Document_InsertLink_Before;
}

private void Document_Insert_After(object sender, DocumentEventArgs e)
{
    // Add custom actions here
    try
    {
        //string whereCondition = "LiveStreamingVideoID=SCOPE_IDENTITY()";
        DataSet liveStreamingvideos = DocumentHelper.GetDocuments("Test.LiveStreamingVideo")
            .OnSite("Test")
            .WhereGreaterThan("DocumentCreatedWhen", DateTime.Now.AddMinutes(-1))
            //.Where(new WhereCondition(whereCondition))
            .Result;
        if (liveStreamingvideos.Tables[0] != null && liveStreamingvideos.Tables[0].Rows.Count > 0)
        {
            LiveStreamingController obj = new LiveStreamingController();
            obj.createNodeOnFCM(liveStreamingvideos);
        }
    }
    catch (Exception ex)
    {
        EventLogProvider.LogEvent(EventType.ERROR, "CreateNodeForOnFCMOfLiveStreamingVideo", "Execute(task)", ex.Message);
    }
}

private void Document_InsertLink_Before(object sender, DocumentEventArgs e)
{
    // Add custom actions here
}

}

so can you please tell me that how will i get that ID

Thanks

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on November 13, 2017 13:40

in the DocumentEvetnArgs e you should be able to access the newly created node and page data.

0 votesVote for this answer Mark as a Correct answer

Matt Nield answered on November 13, 2017 15:15 (last edited on November 13, 2017 15:16)

Pritam,

You use the event args as Juraj states to load the page, you should be able to use page.GetValue("DocumentForeignKeyValue") to find the ItemID. When creating the page in API, you should see something like this:

    var parent = DocumentHelper.GetDocument(43, new TreeProvider()); // Go get my root node

    var page = TreeNode.New("DancingGoat.Cafe"); // Create a new cafe
    page.DocumentName = DateTime.Now.ToLongTimeString();
    page.DocumentCulture = parent.DocumentCulture;
    page.SetValue("CafeStreet", "123 Street");
    page.SetValue("CafeCity", "London");
    page.SetValue("CafeCountry", "United Kingdom");
    page.SetValue("CafeZipCode", "12345");
    page.SetValue("CafePhone", "01234567890");
    page.DocumentPageTemplateID = 25736;

    page.Insert(parent); 

    Debug.WriteLine(page.GetValue("DocumentForeignKeyValue")); // Get the ItemID back

In your event handle, e.Node.GetValue("DocumentForeignKeyValue") should work. The only thing I'm uncertain of is that your'e doing it before insert, so I don't think it has been saved to the DB yet adn won't have an ID.

2 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.