This project is read-only.

Questions about Id and IdentityPart

Topics: Customizing Orchard, General, Writing modules
Jan 11, 2012 at 6:34 PM

I have a custom content part and corresponding type. I found that in order to get a value into the "Id" attribute in the generated XML from the Export feature is to add an IdentityPart to my CustomType. This generates a GUID. Without identity part I wouldn't be able to differentiate the instances from one another and importing data would not be possible. 

I need to import a bunch of data from an existing database and I planned to do it by formatting it into xml that matches Orchard's Import/Export schema, and then importing it through a recipe. My question is, does it matter what value I use for the Id attribute? Is that value stored in the database, and can I name it anything I want or does it have to be a GUID type value? I can guarantee the values to be unique -- the db I'm importing from has an integer identity column, and assuming my Type is called "Shoe", could I assign Id's like this: "Shoe_1234" where 1234 is the unique integer ID from the original DB? 

Also, if I were to just create an integer ID in the db for my ShoePart, is there a way for me to set it so ShoePart's integer "id" property gets set to the <ShoeType>'s Id attribute in the XML export?

Jan 19, 2012 at 6:52 AM

No, the export id is built from many parts. The identity part is just one of those, and it's there for those cases where there isn't a better method. Another is routable, that uses the path as a unique id. They important thing to understand is that you need a form of unicity that an integer can't provide.

Jan 19, 2012 at 7:42 PM

Thanks. I got this working eventually by looking at the export of parts that had RoutePart, and mimicking that. I used the guid() function in my legacy SQL Server database to assign an arbitrary guid to each item. Then I used a "for xml" query to select the data into the format of ORchard's recipes, and imported that result. Also slugified my data items' names in order to set the content part routes in the XML. Eventually I got XML that looked like this: 

<MyContentType Id="/Identifier=a1029b5968cdef2978420dsd9234jhdf/Route.Slug=mySlug Status="Published"><MyContentPart>...

It seems like a forward slash is used here as a delimiter between the different parts of the export Id. I thought it would break if my Route slug contained forward slashes but it seemed to work just fine.  

Jan 19, 2012 at 7:42 PM

And I also see that the values for the Identity Part's identifier value don't necessarily have to be a GUID. The HtmlWidgets created in the default Orchard recipe get assigned values like "SetupHtmlWidget1", and "SetupHtmlWidget2". If you don't set the identifier value, a GUID is generated for you.