• Decrease Text SizeIncrease Text Size

Update Records in the module.

Posted Date: 5/3/2023
    Printer Friendly Version   Email A Friend   Add This   Increase Text Size   Decrease Text Size
In this example, we update one record.


//need using statement for Centralpoint.Web.Cms
string recordTitle = tbRecordTitle.Text;
string recordCaption = tbRecordCaption.Text;

CpCollection attributes = new CpCollection();
CpCollection attributes = new CpCollection();
attributes.Load(CpSystem.NavigationItemInfo("GenericB").Attributes);

//to update a record pass a DataId
attributes.DataId = Guid.NewGuid();
attributes.Set("Title", recordTitle);
attributes.Set("Caption", recordCaption);
attributes.Set("LegacyData", "myTest");

attributes.Set("StartDate", DateTime.UtcNow.ToString());
attributes.Set("EndDate", Centralpoint.Utilities.General.SqlMaxDate.ToString());

CpSystem system = new CpSystem();
system.ConsoleUtilities.UpsertDocument(attributes.Serialize(), this.User.UserId, this.User.Name, this.User.IsAncestorAdmin);

//To create a Version of the document Use UpsertDocumentAndVersion
//system.ConsoleUtilities.UpsertDocumentAndVersion(attributes.Serialize(), this.User.UserId, this.User.Name, this.User, 1, "Record was updated from /SDK/Retrieve_Update_Records.aspx page.");
   


In this example, We Update a Record using batch Insert. (This is the fastest way to update a large number of records.)
This example will set 2 GUIDs for all of the records we retrieved from cpsys_DataCurrent table.


       string cp8Connect = @"Data Source=SERVER_NAME;Initial Catalog=DATABASE_NAME;User Id=USER_ID;Password=PASSWORD;";
       DataSet cp7 = new DataSet("Import");
       Guid batchId = new Guid("0d2060b5-f1eb-4cc5-b9dc-e7ab70e113b3");
       DataCurrent data = new DataCurrent(connect);
       data.CreateBatch(batchId);
       
       using (SqlConnection connect = new SqlConnection(cp8Connect))
        {
            //id of the module we want to update records in.
string moduleId = "50d1763e-67ab-4704-9382-a774f35aea66";
cp7.Tables.Add(Utilities.Database.GetDataTable("MarketplaceDirectoryA", 
    String.Format("SELECT StartDate,EndDate,DataId,ModuleId,AutoNumber,ModifyDate,CreateDate,Attributes From cpsys_DataCurrent Where ModuleId='{0}'", moduleId), connect));

    		 string newTaxonomy = "cd711831-7321-4614-91d0-c0ff77c43ffd, cd1e0ee6-4825-4800-a35a-c06d2feeb08e";
       foreach (DataRow row in ds.Tables["MarketplaceCompany"].Rows)
        {
             Guid dataId = (Guid)row["DataId"];
          int autoNumber = (int)row["AutoNumber"];
          string attributesXmlString = row["Attributes"].ToString();
          Guid moduleId = (Guid)row["ModuleId"];
				
                CpCollection attributes = new CpCollection();
          attributes.Load(attributesXmlString);
          attributes.DataId = dataId;
          attributes.ModuleId = moduleId;
          attributes.Set("StartDate", Management.User.ConvertTimeToUtc((DateTime)row["StartDate"]).ToString());
          attributes.Set("EndDate", Management.User.ConvertTimeToUtc((DateTime)row["EndDate"]).ToString());
          //Taxonomy ids must be unique.
          attributes.Set("Taxonomy", newTaxonomy);

          data.AddToBatch(batchId, attributes, autoNumber, Management.User.ConvertTimeToUtc((DateTime)row["CreateDate"]), DateTime.Now);
              }
        }
       
        data.ImportBatch(batchId, cp8Connect);
     



Keywords: Update Records,UpsertDocument, UpsertDocumentAndVersion



Related Taxonomy

Comments:

Be the first to leave a comment.
Please Login to post comments.