Sitecore:how to get workflow items programmatically - c#

i have assigned workflow as usual. i tried following code but didn't get any item....please share any sample code if you have any or help me get items
Database masterDatabase = Factory.GetDatabase("master");
Item currentItem = masterDatabase.Items["/sitecore/content/Home"];
IWorkflow workflow = masterDatabase.WorkflowProvider.GetWorkflow(currentItem);
WorkflowState state = workflow.GetState(currentItem);

Try to use SecurityDisabler
using (new SecurityDisabler())
{
var db= Factory.GetDatabase("master");
var item = db.Items["/sitecore/content/Home"];
// getting the item's workflow reference
var wf = this.database.WorkflowProvider.GetWorkflow(item);
// here we need either to login as a user with appropriate permissions
// to have access to workflow states/commands or disable security
wf.Start(item);
}

That API is only going to work in a site context where workflow is enabled. Either enable it in your <sites> config or use a context switching block around your code.

Related

How to set Context Id?

I am new with Elsa workflow and I am trying to set Context ID from the designer by the id I get from HTTP Endpoint I created but I do not the syntax that I should use. I tried the JS syntax but it return an error for me.
context.ContextId = WorkflowContext.Id;
First of all, you have to understand how elsa loading context work.
when you enter the type of your workflow context it automatically make loading for your workflow Context with null value in context Id.
so you can not access your Workflow Context so what should you do?
you have first to access your HTTP Endpoint body to get your the information that you want.
using JS syntax
input.Body.Id
then you have to reload your workflow context in the next activity

How do I start a previously stopped Azure Container instance using C#?

I have the following code to stop an Azure container instance and would like to start it using similar.
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;
var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal("XXXX", "XXXX", "XXXX", AzureEnvironment.AzureGlobalCloud);
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithSubscription("XXXXX");
var containerName = "mycontainer";
var containerGroup = azure.ContainerGroups.GetByResourceGroup("myResourceGroup", containerName);
if (containerGroup.State == "Running")
{
containerGroup.Stop();
}
I would like to do the same and start my azure container instance. So where is containerGroup.Start(); ? This does not appear to exist in the interface. I have tried using containerGroup.Restart(); but this does not work from a stopped state. I need to be able to do this from within C# code and would like to avoid powershell if possible.
There is a way to do this but it is not exposed in the fluent API:
using Microsoft.Azure.Management.ContainerInstance.Fluent;
// azure is an instance of IAzure; the fluent Azure API
var resources = await azure.ContainerGroups.ListAsync();
foreach(var containerGroup in resources.Where(aci => aci.State != "Running"))
{
await ContainerGroupsOperationsExtensions.StartAsync(
containerGroup.Manager.Inner.ContainerGroups,
containerGroup.ResourceGroupName,
containerGroup.Name);
}
As mentioned by other people, you do need to realize that this is effectively starting a fresh container. No state will be maintained from the previous run unless you persisted that somewhere else like in a mounted volume.
You'll also need to grant the appropriate rights to whom ever is executing this code. I'm using a function so I had to setup a service account and a role, this blog post has all the details.
Update
The code I'm using is in on GitHub: https://github.com/alanta/azure_scheduler/blob/master/src/StartACIs.cs
Unfortunately, when you stop the container instances, they would be in the Terminated state and you cannot start them again.
Terminated or deleted container groups can't be updated. Once a
container group has stopped (is in the Terminated state) or has been
deleted, the group is deployed as new.
Even if you update the ACI, it also means the ACI would be redeployed. You can take a look at Update containers in Azure Container Instances. In addition, the Restart action also works when the container instances are in the running state.
So there is no start function in the C# SDK for you, at least now. Hope this will help you.
Update
Take a look at the event:
Each time when you start the container group after stop, the container group always these steps: pull the image -> create the container group -> start the container instances. So it’s clear, the container group was recreated when you start it after stop.

Unexpected Behaviour while trying to update database in consecutive requests - EF 6

I have been dealing with this for days
Summary
I am creating a Social site that will be the back bone for another web application. The hangup is when I submit a request to create a group all goes well, but if I attempt to submit this form again with different data I get a DbEntityValidationException. The exception is related to the ApplicationUser entry.
Details
When I start the Application in Debug mode and submit the Group creation form for the first time it will succeed, adding all the entities into the database as excepted. I have verified this and all looks good. While in the same Debug session, I change the information in the form, to create another group, and submit the form, which leads to the DbEntityValidationException.
The error is related the when I try to insert a SocialGroupMemberModel which contains a reference to the User, and other details related to the users status in the group. The User entry is being marked as added and EntityFramework is trying to insert the User instead of updating. I have attempted to set the Navigation (User) and set the ForeignKey (UserId), both lead to the same error.
I am using HttpContext.Current.GetOwinContext().Get<ApplicationDbContext>();
In the Controller I use ApplicationUserManager to get the User Entity, I then pass this to the Repository to create the group (in either case, either passing the ID, or Entity itself, doesn't work the second time)
Group Creation Code:
var groupInfo = new SocialGroupInfo
{
Created = DateTime.Now,
Description = model.Description,
ShortDescription = model.ShortDescription,
Name = model.Name,
Tags = TagRepo.GetTags(),
Members = new List<SocialGroupMember>()// { member }
};
var groupModel = new SocialGroupModel
{
Slug = model.Slug,
Info = groupInfo
};
Context.SocialGroups.Add(groupModel);
var member = new SocialGroupOwnerModel
{
Joined = DateTime.Now,
UserId = creator
//User = null
//Group = groupInfo
};
groupInfo.Members.Add(member);
//creator.SocialGroups.Add(member);
SaveChanges();
The Validation Error is: "User name ** is already taken" so this leads me to believe that on the second attempt to add the new group, it is attempting to add a new user.
Please ask for any additional information needed, thanks.
This issue was caused by the IoC holding a reference to the previous DbContext, unsure as to why, but removing all usage of Autofac fixed the issue.
Very anticlimactic solution, but issue fixed...
Now the issue is to figure out why Autofac was behaving this way, all Debugging showed that the classes were created each request... but that is another question.

Set InArgument when launching a workflow programmatically

Is there any way to set the value of a workflow's InArgument parameter from another workflow when launching it programmatically ?
Here's how I launch it:
var req = new ExecuteWorkflowRequest { WorkflowId = new Guid(WorkflowGuids.RenewalWorkflowId), EntityId = theContact.Id };
service.Execute(req);
I can catch the EntityId value back in the other workflow in context.PrimaryEntityId but I am at a loss as to how to populated the arguments and retrieve them on the other side.
Is this possible ? Thanks.
InArgument are defined at step level, not at workflow level.
When a workflow is executed (by a trigger, on-demand or by code) you have the record Id.
You can create a custom workflow activity to fetch other data related (or not connected at all) with your record Id and make it as OutArgument so will be available as input for the InArgument you want to set.

Kendo UI Scheduler connection to database

I'm currently developing a web app using the open source Kendo UI Scheduler with ASP.NET MVC Visual Studio 2012. But i'm experiencing some trouble while trying to connect my scheduler with a local database to store the bookings made by users of my application.
I've been looking for documentation to help me with this but I haven't been able to set this up entirely...
I followed instructions about this on: http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/scheduler/ajax-editing
I've added the Entity Framework Data Model and necessary code in Models like TaskViewModel but the problem occurs in my Controllers.
public ActionResult Tasks_Read([DataSourceRequest]DataSourceRequest request)
{
using (var sampleDB = new SchedulerEntities())
{
IQueryable<TaskViewModel> tasks = sampleDB.Tasks.ToList().Select(task => new TaskViewModel()
{
TaskID = task.TaskID,
Title = task.Title,
//Specify the DateTimeKind to be UTC
Start = Convert.ToDateTime(task.Start),
End = Convert.ToDateTime(task.End),
Description = task.Deschription,
}).AsQueryable();
return Json(tasks.ToDataSourceResult(request));
}
}
I get an error on the DataSourceRequest: the type or namespace DataSourceRequest could not be found. But I can't find to narrow down which module I'm missing or what else I'm doing wrong...
Besides that I also get the following error System.LinqIQueryable doest not contain a definition for ToDataSourceResult. on the code:
return Json(tasks.ToDataSourceResult(request));
Anyone who can help me here or has an other/better solution to make a connection to a local database using the open source Kendo UI Scheduler?
Any help would be really appreciated!
You need to specify the allow get behavior.
return Json(tasks.ToDataSourceResult(request), JsonRequestBehavior.AllowGet));

Categories

Resources