Kentico UserInfoProvider not working as expected in a console app

早过忘川 提交于 2019-12-02 21:42:26

问题


This code works fine within a Kentico website:

var users = UserInfoProvider.GetUsers();
for (int x = 0; x < users.Count(); x++
{
    UserInfo currentUser = users.ElementAt(x);
    currentUser.SetValue("AcceptsAlerts", equivalentSubscriber.Status != SubscriberStatus.Unsubscribed);
    UserInfoProvider.SetUserInfo(currentUser);
}

When I move the code to a console app, any calls to UserInfoProvider result in the error: "Object type 'cms.usersettings' not found"

For the initial call to get the users, I can do it like this in the console app:

DataSet usersds = new CMS.DataEngine.DataQuery("cms.user.selectall").Execute();

then loop through Table1 of the dataset using the user data:

UserInfo currentUser = new UserInfo(dtUsers.Rows[x]);

All is fine and working, until I come to write the updated user back to the database. I cannot find another way of writing the data apart from calling:

UserInfoProvider.SetUserInfo(currentUser);

Does anyone know another way to save the user data? or to resolve the error. The error is a runtime error and as far as I know, I have referenced everything I need to. The field I am editing is a custom field added to the cmsUser table.

using statements for info:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using CMS;
using CMS.CustomTables;
using CMS.DataEngine;
using CMS.Membership;

回答1:


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().




回答2:


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();
        }
    }
    



回答3:


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!



来源:https://stackoverflow.com/questions/28325858/kentico-userinfoprovider-not-working-as-expected-in-a-console-app

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