Kentico UserInfoProvider not working as expected in a console app - c#

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;

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
Reference in your Console application these assemblies from lib folder
CMS.Base
CMS.DataEngine
CMS.DataProviderSQL
CMS.Membership
Then copy your Connection String from Web Application's web.config to Console Application's App.config.
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!

Related

what reference I need to add in C# to get LaunchPadApi?

I try to connect to the API (Using C#) that client provided, but it is using LaunchPadApi() I am not sure what assembly I need to add to get this, these are 2 references that were added:
using IO.Swagger.Api;
using IO.Swagger.Client;
I was able to find using IO.Swagger.Client; but I am not able to find using IO.Swagger.Api;
This is a sample code I am not sure how I can get LaunchPadApi defined so "responsePost" method is not recognized as well,
using System;
using System.Diagnostics;
using IO.Swagger.Api;
using IO.Swagger.Client;
using IO.Swagger.Model;
public class responsePostExample
{
public void main()
{
var apiInstance = new LaunchPadApi();
....
try
{
apiInstance.responsePost(body, xAPIKey);
}
....
Install Launchpad in your nuget package manager for the project
Add using Launchpad; to the top of your file

Branch TFS Directory and Check-in

Been looking all day for a solution for this, with little to no luck. I have this Windows Forms app in C# - My goal is to branch a directory that is in my TFS repository, make changes to filenames/folder names within the newly created branch if possible, and check code back into TFS with updated folder name/filename structure.
Is there a package or a class that I'm not seeing that would allow me to create and manipulate branches, as well as checking in the code all from a c# winforms app? If so, an example would be much appreciated.
If you are using Client Object Model Reference to manage the Version Control programmatically.
To create a branch, you need to use the "CreateBranch()" method in Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer class.
Creates a branch on the server and checks it in without downloading
the branch to the client.
A sample for you reference:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.Client;
using System.Net;
namespace Model.versionControl
{
public static class CreateBranch
{
public static void CreateBranchWithComment()
{
NetworkCredential cre = new NetworkCredential("userName", "password", "domain");
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("http://TFSServerName:8080/tfs/CollectionName"), cre);
VersionControlServer vcServer = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
int changesetId = vcServer.CreateBranch(#"$/SourceControl/WebSites", "$/SourceControl/WebSites_Branch", VersionSpec.Latest);
new WorkspaceVersionSpec("machineName","domain\userName");
Changeset changeset = vcServer.GetChangeset(changesetId);
changeset.Update();
}
}
}
You could also take a look at this similar question: How to create a new source code branch using TFS API?
As for renaming a file or directory, you could use Workspace.PendRename Method, sample code please refer: How do I move a TFS file with c# API?

System.ArgumentNullException: 'Value cannot be null.'

I am relatively new to C# and i have come across this error while working on a project to spin a VM (and support resources in MS Azure).
The code I am using is the one below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Management.Compute.Fluent;
using Microsoft.Azure.Management.Compute.Fluent.Models;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
namespace ConsoleApp1
{
class Program
{
private static void Main(string[] args)
{
var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();
Also I have an "azureauth.properties.txt" file i used to set a new Environmental path referenced in the code above as "AZURE_AUTH_LOCATION".
To set the path i used the simple PS command:
[Environment]::SetEnvironmentVariable("AZURE_AUTH_LOCATION", "C:\MY-PATH\azureauth.properties", "User")
The azureauth.properties file contains simple Tenant/application/key IDs in the format
subscription=<subscription-id>
client=<application-id>
key=<authentication-key>
tenant=<tenant-id>
managementURI=https://management.core.windows.net/
baseURL=https://management.azure.com/
authURL=https://login.windows.net/
graphURL=https://graph.windows.net/
Whenever I am trying to run the project i get an error saying:
System.ArgumentNullException: 'Value cannot be null.'
specifically for the line:
var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));
Any idea why?
Like luxun said, you have to define the environment variable.
For that open the cmd (on windows) and write:
set AZURE_AUTH_LOCATION
this will show the environment variable "AZURE_AUTH_LOCATION". If the result is "Environment variable AZURE_AUTH_LOCATION not defined" or if the path is wrong then write on the cmd:
SET AZURE_AUTH_LOCATION=PathOfTheAzureAuthLocationFile
This should do the trick.
Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION") must be returning null.
If this is the case you may want to check if that environment variable has actually been defined or whether the file exists.
Define a variable like:
var location = Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION");
When you debug in VS you should be able to inspect the value of location by right clicking on it and clicking quickwatch.
Many thanks for your suggestions.
in the end the error was generated by a school-boy mistake in setting the Env Variable in the first place (relative path contained an error I didn't spot earlier).
I was able to run it in the end.
Made a change to define the path in the config file instead too.
thanks

C# adding table object to entity and saving changes not working

I'm learning about ADO.Net entity framework, and
I'm stuck at adding entity table object to the database.
I have a local database in solution called testDB.
It has two columns - id(primary, unique, identiy),name(varchar(100))
and entity for it. The main application code is here below.
Problem is, that using this code it doesn't add anything to the table, but also
I'm not having any errors.
What could go wrong?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace entityproject
{
class Program
{
static void Main(string[] args)
{
String someString;
someString = "Just a test";
testDBEntities tdbEntity = new testDBEntities();
test tblTest = new test();
tblTest.name = someString;
tdbEntity.test.Add(tblTest);
tdbEntity.SaveChanges();
}
}
}
App.config here
http://pastie.org/6980938
I think I know the problem - had the same thing last time I tried using SQL CE.
The connection string doesn't point to the sdf file that you created - it uses a new one that I believe gets put into your bin\Debug or bin\Release directory (in your config file as data source=|DataDirectory|\testDB.sdf), with your EXE and DLLs. If you check that directory, I bet you'll find another sdf file there, that has a bunch of records added.
If you want to use the sdf that you've already created, change the connection string to point specifically to that file.

How to obtain Handle.ToInt32() in an ASP.NET web application

I am trying to learn and use an SDK for a vendor's product. Unfortunately, the documentation is sketchy and I've run into a void in my own knowledge of the .Net Framework.
I have some working code for a windows forms application and I am trying to get it working in an ASP.NET web form app. The vendor documentation implies you can do this but maybe you cannot..
Snippet from the working windows app:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using TRIMSDK;
private void ConnectUserBtn_Click(object sender, System.EventArgs e)
{
Database db = new Database();
Databases dbChooser = new Databases();
IDatabase dbI = dbChooser.ChooseOneUI(Handle.ToInt32());
if (dbI == null)
{
return;
}
db.Id = dbI.Id;
Now here is my attempt inside click event handler for an .ASPX page:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TRIMSDK;
protected void ConnectUserBtn_Click(object sender, EventArgs e)
{
Database db = new Database();
Databases dbChooser = new Databases();
IDatabase dbI = dbChooser.ChooseOneUI(Handle.ToInt32());
if (dbI == null)
{
return;
}
I get a compile complaint in the line just above that reads "The name 'Handle' does not exist in the current context.
This part of the SDK I am trying to use displays various modal dialogs that reflect the properties of the product to facilitate "client" development. I fear it might be only "Windows clients" and that ASP.NET web apps cannot do this.
Is there something I can add to resolve this?
For web apps, modal dialogs would be done at the client, usually via javascript and dhtml - not at the server (where ASP.NET code executes). So I fear that this product is indeed winforms only.
Just pass it:
int hwnd = 0;
IDatabase dbI = dbChooser.ChooseOneUI(hwnd);

Categories

Resources