Creating new smartform data using Ektron ContentTypes - ektron

Ektron 8.0.1 SP1
I am using SmartForms and Content Types to read (and hopefully write) data. I can read data but now I am attempting to write a new record similar to the following.
ContentTypeManager<member> contentTypeManager = new ContentTypeManager<member>();
ContentType<member> newmem = new ContentType<member>();
newmem.SmartForm.details.field1 = "Chuck"; // This line throws 'Object reference not set to an instance of an object.' error
newmem.SmartForm.details.field2 = "Norris";
contentTypeManager.Update(newmem);
I get the error "Object reference not set to an instance of an object." for that first assignment line. What am I missing?
I am having trouble finding good documentation on ContentTypes for 8.0.1 now that the Ektron website has been redesigned.
Thx.

Thanks for clarifying, to ADD content to a folder that has a smartform assigned to it, the basic code block should get you started: (Note: the Html attribute of the content is simply the xml matched to the schema you created)
Ektron.Cms.Framework.Content.ContentManager cmanager = new Cms.Framework.Content.ContentManager();
Ektron.Cms.ContentData cdata = new ContentData();
cdata.FolderId = 0;
cdata.XmlConfiguration.Id = 0; //SMARTFORM ID HERE
cdata.Html = "<root><field1>field1 value</field1><field2>field2 value</field2></root>";
cmanager.Add(cdata);

You could update ContentTypes.cs to include an Add method. Just copy the Update method and change contentManager.Update to contentManager.Add.
public void Add(ContentType<T> contentType)
{
Initialize();
contentType.Content.Html = Ektron.Cms.EkXml.Serialize(typeof(T), contentType.SmartForm);
contentManager.Add(contentType.Content);
}
Unfortunately, contentManager.Add returns void. Ideally it should return the new content ID.

Related

Cosmos DB "Partial Update"/Patch, cant set new property value to null

I'm trying out the Cosmos DB SDK's new Patch/Partial Update-functionality (for .NET)
When adding a new property I use
var patchOperations = new List<PatchOperation>(){
PatchOperation.Add<string>("/FavoriteColor", **null**)
};
await container.PatchItemAsync<T>(
id: myId,
partitionKey: new PartitionKey(myPk),
patchOperations: patchOperations);
The problem is, that it throws at the PatchOperation-Add() if I set second parameter to null (with message "Value cannot be null"). I can set any non-null string and it works well. I just wonder if this isn't supported yet or if I missed something.
Thanks to github user rvdvelden (source), this work around appears to work perfectly:
private JToken NullValue { get { return new JProperty("nullValue", null).Value; } }
Used in this way:
operations.Add(PatchOperation.Set("\customer", value ?? NullValue));
Remove is one alternative if the intent is to remove field/property.
This is not supported with Patch yet,
However, if you want to remove the entire property you need to use Remove Operation

Google diff-match-patch : How to unpatch to get Original String?

I am using Google diff-match-patch JAVA plugin to create patch between two JSON strings and storing the patch to database.
diff_match_patch dmp = new diff_match_patch();
LinkedList<Patch> diffs = dmp.patch_make(latestString, originalString);
String patch = dmp.patch_toText(diffs); // Store patch to DB
Now is there any way to use this patch to re-create the originalString by passing the latestString?
I google about this and found this very old comment # Google diff-match-patch Wiki saying,
Unpatching can be done by just looping through the diff, swapping
DIFF_INSERT with DIFF_DELETE, then applying the patch.
But i did not find any useful code that demonstrates this. How could i achieve this with my existing code ? Any pointers or code reference would be appreciated.
Edit:
The problem i am facing is, in the front-end i am showing a revisions module that shows all the transactions of a particular fragment (take for example an employee details), like which user has updated what details etc. Now i am recreating the fragment JSON by reverse applying each patch to get the current transaction data and show it as a table (using http://marianoguerra.github.io/json.human.js/). But some JSON data are not valid JSON and I am getting JSON.parse error.
I was looking to do something similar (in C#) and what is working for me with a relatively simple object is the patch_apply method. This use case seems somewhat missing from the documentation, so I'm answering here. Code is C# but the API is cross language:
static void Main(string[] args)
{
var dmp = new diff_match_patch();
string v1 = "My Json Object;
string v2 = "My Mutated Json Object"
var v2ToV1Patch = dmp.patch_make(v2, v1);
var v2ToV1PatchText = dmp.patch_toText(v2ToV1Patch); // Persist text to db
string v3 = "Latest version of JSON object;
var v3ToV2Patch = dmp.patch_make(v3, v2);
var v3ToV2PatchTxt = dmp.patch_toText(v3ToV2Patch); // Persist text to db
// Time to re-hydrate the objects
var altV3ToV2Patch = dmp.patch_fromText(v3ToV2PatchTxt);
var altV2 = dmp.patch_apply(altV3ToV2Patch, v3)[0].ToString(); // .get(0) in Java I think
var altV2ToV1Patch = dmp.patch_fromText(v2ToV1PatchText);
var altV1 = dmp.patch_apply(altV2ToV1Patch, altV2)[0].ToString();
}
I am attempting to retrofit this as an audit log, where previously the entire JSON object was saved. As the audited objects have become more complex the storage requirements have increased dramatically. I haven't yet applied this to the complex large objects, but it is possible to check if the patch was successful by checking the second object in the array returned by the patch_apply method. This is an array of boolean values, all of which should be true if the patch worked correctly. You could write some code to check this, which would help check if the object can be successfully re-hydrated from the JSON rather than just getting a parsing error. My prototype C# method looks like this:
private static bool ValidatePatch(object[] patchResult, out string patchedString)
{
patchedString = patchResult[0] as string;
var successArray = patchResult[1] as bool[];
foreach (var b in successArray)
{
if (!b)
return false;
}
return true;
}

RavenDB - deleting previously-conflicted documents with "/conflicts" in URL

I’m trying to programmatically delete once-conflicted documents that are no longer conflicted, but still showing as duplicates in Raven.
E.g. if I query an index on an entity’s property I know to be unique I get two documents back
Document A with URL entities/12345
Document B with URL entities/12345/conflicts/54321
My goal is to delete Document B.
Loading document A into a session does not throw a ConflictException, as it is not flagged as being conflicted any more. I can delete document B via the web UI, but can’t do it via code as yet, as I can only see it in transient context via a stream.
Here’s some sample code which explains what I am getting back from various client calls when trying to resolve this…
using (var enumerator = session.Advanced.Stream(query))
{
while (enumerator.MoveNext()))
{
var entity = enumerator.Current.Document;
// This attempt to get the id returns null
var id = session.Advanced.GetDocumentId(entity);
// Throws InvalidOperationException
var url = session.Advanced.GetDocumentUrl(entity);
// Returns null, so can’t use session to delete
session.Load<TEntity>(entity);
// Does nothing, with string ID of entity
session.Advanced.Defer(new DeleteCommandData { Key = entity.Id.ToString() });
// Does nothing
session.Advanced.DocumentStore.DatabaseCommands.Delete(entity.Id.ToString(), null);
}
session.SaveChanges();
}
Any help would be gratefully received!
I found that using enumerator.Current.Key in the example above gave me the key to the document I was after.

Sitecore Glass mapper GetItem<TypeName>(guid) always return null

I saw a related question:
Sitecore Glass Mapper always null
But unfortunately it does not give a solution for my case.
Here goes a code snippet:
var db = Factory.GetDatabase("master");
var context = new SitecoreContext();
// the ID of Needed item
var g = new Guid("{F21C04FE-8826-41AB-9F3C-F7BDF5B35C76}");
// just to test if it's possible to fetch item using db.GetItem
var i = db.GetItem(new ID(g), Language.Current, Sitecore.Data.Version.Latest);
// Grab item
var t = context.GetItem<Article>(g);
In the code above:
i is not null
t is null
Article is the simple class like:
[SitecoreType(TemplateId = "{4C4EC1DA-EB77-4001-A7F9-E4C2F61A9BE9}")]
public class Article
{
[SitecoreField(FieldName = "Title")]
public string Title { get; set; }
}
There are only one language installed in Sitecore - en, it has been specified in the web.config in the items as well.
Also I have added GlassMapperSc.Start(); to Application_Start in the Global.asax.cs and added my assembly to the list of included assemblies via var attributes = new AttributeConfigurationLoader(new[] { "Assembly.Name" }); and I succeeded to find my class in the SitecoreContext mappings.
It does not looks like a language issue, as stated in the link provided in the very beginning. And I'm struggling with it already for a pretty long time, but no luck...
Thank You!
I just noticed that you are using master db for the Sitecore DB and SitecoreContext for Glass.
The SitecoreContext class will use the database that is defined by the Sitecore.Context.Database property at runtime. This probably means that it is using the web database.
Can you check that you have published the item to the web database or instead using:
var context = new SitecoreService("master");

Problem saving a new item to custom SharePoint 2007 list using Silverlight 4 UI control

I am hoping someone might be able to help me. I'm working on a Contact Manager built using a custom SharePoint 2007 list with a Silverlight 4 UI embedded in a content editor web part.
I am currently able to retrieve the data from the list and display it in a datagrid on the UI and everything works well.
Now I am trying to add the the ability to add new items to the list using the following code but the items do not save.
I've remotely debugged the following code using the Debug -> Attach to Process option and everything seems to execute successful without any errors but it does not save the item to SharePoint.
In order to simplify and get a working insert function I changed all the SharePoint fieds to single line text with the exception of the notes (multiline) and none of the fileds are required.
The sharepoint site does require Windows authentication but it seems to be working correctly as I am able to display it as well as add new items manually using the standard SharePoint forms.
Lastly, I have added the xml for the Batch element at the bottom which I copied as output while debuging.
Please let me know if there is any additional information I might be missing.
Thanks in advance for any assistance you might be willing to provide.
Charles
public string sharepoint_soap_namespace = "http://schemas.microsoft.com/sharepoint/soap/";
public string sharepoint_rowset_namespace = "#RowsetSchema";
public string service_lists_url = "http://myDomain/_vti_bin/lists.asmx";
public string listName = "MyContacts";
public void TestCreateContact()
{
Uri serviceUri = new Uri(service_lists_url);
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
binding.MaxReceivedMessageSize = 2147483647; // This has to be the same as in the ServiceReferences.ClientConfig file.
EndpointAddress endpoint = new EndpointAddress(serviceUri);
ListsSoapClient testCreateClient = new ListsSoapClient(binding, endpoint);
XElement batch = new XElement("batch",
new XElement("Method",
new XAttribute("ID", "1"),
new XAttribute("Cmd", "New"),
CreateFieldElement("ows_ID", "New"),
CreateFieldElement("ows_Title", "John"),
CreateFieldElement("ows_SupportFor","USA"),
CreateFieldElement("ows_LastName","Doe")
));
testCreateClient.UpdateListItemsCompleted +=
new EventHandler<UpdateListItemsCompletedEventArgs>(createSoapClient_UpdateListItemsCompletedEventArgs);
testCreateClient.UpdateListItemsAsync(listName, batch);
testCreateClient.CloseAsync();
}
private XElement CreateFieldElement(string fieldName, string fieldValue)
{
XElement element = new XElement("Field",
new XAttribute("Name", fieldName),
fieldValue);
return element;
}
Just a quick update to let everyone know I was able to answer my own question.
It seems that in the batch XElement I was using the wrong field names.
CreateFieldElement("ows_SupportFor","USA"),
I was using "ows_SupportFor" instead of "SupportFor" without the "ows_" prefix.
Cheers,
Charles