Kentico UserInfoProvider not working as expected in a console app

亡梦爱人 提交于 2019-12-02 09:05:12

Before you start working with Kentico CMS API from an external application make sure you call the following lines:

CMS.DataEngine.ConnectionHelper.ConnectionString = "your connection string";
CMS.Base.SystemContext.WebApplicationPhysicalPath = Application.StartupPath;
CMS.DataEngine.CMSApplication.Init();

Then, you'll be also able to use UserInfoProvider.GetUsers() object query instead of using DataQuery.Execute().

Are you sure you are referencing all necessary assemblies?

Following scenario works on my machine with configuration: Kentico 8.x, Web Application project

  1. Reference in your Console application these assemblies from lib folder

    • CMS.Base
    • CMS.DataEngine
    • CMS.DataProviderSQL
    • CMS.Membership
  2. Then copy your Connection String from Web Application's web.config to Console Application's App.config.

  3. After that you can use this code to set custom user properties

    static void Main(string[] args)
    {
        var users = UserInfoProvider.GetUsers();
    
        foreach (var user in users)
        {
            user.SetValue("myTestString", "test");
            user.Generalized.SetObject();
        }
    }
    

For anyone looking to get a SiteID to use in API calls from an external app such as getting an email template, this might help you. In Kentico 8.1 you can go to Sites > General and get the code name for your site. Then you can do this:

int siteID = CMS.SiteProvider.SiteInfoProvider.GetSiteID("<your site code name>");

Hope it helps!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!