Kentico CMS 7.0 Developer's Guide

Code examples

Code examples

Previous topic Next topic Mail us feedback on this topic!  

Code examples

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

The following examples show how you can use the CMS.DataEngine and CMS.SettingsProvider libraries for low-level data manipulation.

 

 

InfoBox_Arrow

 

Only for illustration

 

The code is used only for illustration. It's recommended that you use the CMS.SiteProvider.UserInfoProvider class for manipulation of the user data.

 

 

Creating a new user

 

[C#]

 

using CMS.DataEngine;

...

 

// create a new object based on the cms.user data class

IDataClass userObj = DataClassFactory.NewDataClass("cms.user");

// Set some values. The first parameter of the SetValue method accepts database column names that belong to the particular object.

userObj.SetValue("UserName", "johns");

userObj.SetValue("FullName", "John Smith");

// insert the new user into the database, in this case, the CMS_User table

userObj.Insert();

 

Selecting and updating an existing user

 

[C#]

 

using CMS.DataEngine;

...

 

// prepare the user's ID

int userId = 10;

 

// get the object from database according to the data class name and ID

IDataClass userObj = DataClassFactory.NewDataClass("cms.user", userId);

// acquire the user's name

string userName = userObj.GetValue("UserName").ToString();

// change the user's full name

userObj.SetValue("FullName", "John Smith");

// update the database record

userObj.Update();

 

Deleting a user

 

[C#]

 

using CMS.DataEngine;

...

// prepare the user's ID

int userId = 10;

 

// get the object from the database

IDataClass userObj = DataClassFactory.NewDataClass("cms.user", userId);

// if the object exists, delete it

if (userObj != null)

{

   userObj.Delete();

}

 

Running a custom query

 

You can run a custom query if you first create it either manually in the CMS_Query table or through the administration interface (if this is supported for the given entity).

 

[C#]

 

using CMS.SettingsProvider;

...

QueryDataParameters parameters = new QueryDataParameters();

parameters.Add("@UserName", "administrator");

 

DataSet ds = SqlHelperClass.ExecuteQuery("cms.user.selectbyusername", parameters, null, null);