Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I update a content item (draft) from a background task in Orchard?
    text
    copied!<p>I have a simple <code>IBackgroundTask</code> implementation that performs a query and then either performs an insert or one or more updates depending on whether a specific item exists or not. However, the <em>updates</em> are not persisted, and I don't understand why. <em>New items</em> are created just as expected.</p> <p>The content item I'm updating has a <code>CommonPart</code> and I've tried authenticating as a valid user. I've also tried flushing the content manager at the end of the <code>Sweep</code> method. What am I missing?</p> <p>This is my <code>Sweep</code>, slightly edited for brevity:</p> <pre><code>public void Sweep() { // Authenticate as the site's super user var superUser = _membershipService.GetUser(_orchardServices.WorkContext.CurrentSite.SuperUser); _authenticationService.SetAuthenticatedUserForRequest(superUser); // Create a dummy "Person" content item var item = _contentManager.New("Person"); var person = item.As&lt;PersonPart&gt;(); if (person == null) { return; } person.ExternalId = Random.Next(1, 10).ToString(); person.FirstName = GenerateFirstName(); person.LastName = GenerateLastName(); // Check if the person already exists var matchingPersons = _contentManager .Query&lt;PersonPart, PersonRecord&gt;(VersionOptions.AllVersions) .Where(record =&gt; record.ExternalId == person.ExternalId) .List().ToArray(); if (!matchingPersons.Any()) { // Insert new person and quit _contentManager.Create(item, VersionOptions.Draft); return; } // There are at least one matching person, update it foreach (var updatedPerson in matchingPersons) { updatedPerson.FirstName = person.FirstName; updatedPerson.LastName = person.LastName; } _contentManager.Flush(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload