Duplicating content on save for a multilingual umbraco site - c#

[Edit] I have actually been allowed to use the doc names, which makes it much easier but I still think it would be interesting to find out if it is possible.
I have to set a trigger to duplicate content to different branches on the content tree as the site will be in several languages. I have been told that I cannot access the documents by name(as they may change) and I shouldn't use node IDs either(not that I would know how to, after a while it would become difficult to follow the structure).
How can I traverse the tree to insert the new document in the relevant sub branches in the other languages? Is there a way?

You can use the Document.AfterPublish event to catch the specific document object after it's been published. I would use this event handler to check the node type alias is one that you want copied, then you can call Document.MakeNew and pass the node ID of the new location.
This means you don't have to use a specific node ID or document name to trap an event.
Example:
using umbraco.cms.businesslogic.web;
using umbraco.cms.businesslogic;
using umbraco.BusinessLogic;
namespace MyWebsite {
public class MyApp : ApplicationBase {
public MyApp()
: base() {
Document.AfterPublish += new Document.PublishEventHandler(Document_AfterPublish);
}
void Document_AfterPublish(Document sender, PublishEventArgs e) {
if (sender.ContentType.Alias == "DoctypeAliasOfDocumentYouWantToCopy") {
int parentId = 0; // Change to the ID of where you want to create this document as a child.
Document d = Document.MakeNew("Name of new document", DocumentType.GetByAlias(sender.ContentType.Alias), User.GetUser(1), parentId)
foreach (var prop in sender.GenericProperties) {
d.getProperty(prop.PropertyType.Alias).Value = sender.getProperty(prop.PropertyType.Alias).Value;
}
d.Save();
d.Publish(User.GetUser(1));
}
}
}
}

Related

How to add parameters to a revit family element from revit document in revit-api c#

I'm working on a project where I need to load pipe fittings (bends) revit families on a revit document and update its shared parameters as type parameters.
I was able to achieve this task since I needed to access the bend family as family instance.
The second part of the requirement is to update the coupling family parameters of the bend as well.
The problem is that I'm unable to access the coupling parameter from the revit document.
If I try it manually, the coupling parameter is accessible only when I double click on the bend family, this opens the revit family file. I then can access both the coupling on either side of the bend.
How can I achieve the above task of adding parameters to a revit family element from revit document programmatically.
Please guide me.
Thank You in advance.
My Code:
foreach (var item in MainModel.listElement)//List<Element>
{
FamilyInstance elFamInst = item as FamilyInstance;
if (elFamInst.Symbol.FamilyName == "MainFamilyName")//Bend Family
{
ElementId id = item.GetTypeId();
ElementType et = doc.GetElement(id) as ElementType;
Family fam = elFamInst.Symbol.Family;
if (elFamInst.SuperComponent == null)
{
var subElements = elFamInst.GetSubComponentIds();
if (subElements.Count != 0)
{
foreach (var aSubElemId in subElements)
{
var aSubElem = doc.GetElement(aSubElemId);
if (aSubElem is FamilyInstance)
{
FamilyInstance subEl = aSubElem as FamilyInstance;
Family fm = subEl.Symbol.Family;
if(subEl.Symbol.FamilyName == "subFamilyName")//coupling family
{
Document docfamily = doc.EditFamily(fam);
if (null != docfamily && docfamily.IsFamilyDocument == true)
{
FamilyManager familyManager = docfamily.FamilyManager;
using (Transaction trans = new Transaction(docfamily, "param"))
{
trans.Start();
FamilyParameter fp = familyManager.AddParameter("Namess",BuiltInParameterGroup.PG_IDENTITY_DATA,ParameterType.Text, false);
familyManager.Set(fp, "JP");
trans.Commit();
}
}
}
}
}
}
}
}
}
I am unable to achieve the result, also I want the parameter "JP" to be set only on the coupling family i.e. the parameter with the name "subFamilyName".
Yes, you can achieve the same programmatically as well via the API.
Just as you need to open the family file in the manual approach, you can call the EditFamily method to do so in the API:
https://apidocs.co/apps/revit/2019/56e636ee-5008-0ee5-9d6c-5f622dedfbcb.htm
Check out various posts by The Building Coder mentioning this method, e.g., Modifying, Saving and Reloading Families.

Is it possible to assign the CategoryID value to CategoryInfo object?

I've been trying to add a new category by using the following block of code:
CategoryInfo category = new CategoryInfo()
{
CategoryID = 999, // manually set
CategoryName = "TestCategory",
CategoryDisplayName = "Test Category",
CategoryEnabled = true,
CategorySiteID = 1
};
CategoryInfoProvider.SetCategoryInfo(category);
This doesn't throw any errors but it doesn't add the new category to CMS_Category table.
However, if I removed this line: CategoryID = 999,, the category gets saved into the system and the CategoryID is automatically assigned.
I would like to set this field manually. Any help is appreciated.
(I am trying to avoid creating additional fields to handle this)
Kentico decides whether to save or update an object based on whether its primary key is set. So if CategoryID is set the system actually calls UPDATE CMS_Category SET ... WHERE CategoryID = #CategoryID instead of INSERT INTO ...
If you need to store the original reference (I'm guessing that you are trying to store an external identifier for integration purposes) I'd suggest storing it in a separate field. And to prevent modifying system fields, I'd utilize the code name field - CategoryName.
Though what #martin suggests seems like a good idea, I'm pretty sure it won't work as Kentico would lose the reference to the object that's currently being updated. If you have a valid source code license, look at the CategoryInfoProvider.SetCategoryInfoInternal() to see what I'm talking about.
I haven`t tried it but you can try to change ID of category after create, something like:
[CustomCategoryID]
public partial class CMSModuleLoader
{
private class CustomCategoryID : CMSLoaderAttribute
{
public override void Init()
{
CategoryInfo.TYPEINFO.Events.Insert.After += Insert_After;
}
void Insert_After(object sender, CMS.DataEngine.ObjectEventArgs e)
{
var category = e.Object as CategoryInfo;
if (category != null)
{
category.CategoryID = 999, // manually set
category.Update();
}
}
}
}
Are you sure the system does not store your new category but with different ID (that you provide)?
Could you please tell me why do you want to customize object ID behavior? Thanks

NRefactory: How do I access unresolved Named Arguments on a Property Attribute?

I apologize in advance for the long description of a simple question but I want to make sure people properly understand what I'm trying to do.
Background
I'm writing a tool that can read in a file generated by SqlMetal and create a class that contains methods for simple Inserting, Updating, Deleting and Selecting, which can then be exposed to a web service. The main advantage here is that if a table changes, I simply have to re-run the tool and the database-related code is automatically updated and everywhere that uses it will generate compile errors, making it easy to track down where manual changes need to be made. For example, if I have a Customer table that has the following fields:
CustomerId (PK, Identity)
FirstName
LastName
I want to be able to generate Insert and Delete methods as follows:
// I only want non-database-generated fields to be parameters here.
public static Customer InsertCustomer(String firstName, String lastName)
{
...
}
// I only want the primary key fields to be parameters here.
public static int DeleteCustomer(int customerId)
{
...
}
I am using SqlMetal to generate a Customer class. Now what I want to do is read that .cs file into my new tool in order to create another class with the above methods. This new class can then be exposed to the web service to grant access to this functionality without having to expose the underlying database. I am using NRefactory to read in the SqlMetal-generated file and so far, it's going well but I've run into a snag trying to read the property attributes on my Customer class.
SqlMetal generates its classes using a ColumnAttribute to identify each property that is derived from a database column. The ColumnAttribute will have a number of arguments to describe the database column's properties. In the above example, it would generate something like this:
...
[Column(Name="customerId", Storage="_CustomerId, DbType="INT NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int CustomerId
{
...
}
[Column(Name="firstName", Storage="_FirstName", DbType="NVarChar(100) NOT NULL", CanBeNull=false)]
public String FirstName
{
...
}
[Column(Name="lastName", Storage="_LastName", DbType="NVarChar(100) NOT NULL", CanBeNull=false)]
public String LastName
{
...
}
...
Problem
As you can see, SqlMetal gives me the attributes I need in order to identify which columns are database-generated and which ones are part of the primary key. So when I read this file into NRefactory and resolve the type, I would expect to be able to get at all of this information. However, I'm finding that while I can get to the ColumnAttribute, all of the arguments on it are unresolved and therefore aren't accessible via the NamedArguments or PositionalArguments properties.
Here's my code:
SyntaxTree syntaxTree = ...;
foreach(AstNode tableNode in syntaxTree.Children)
{
ResolveResult result = resolver.Resolve(tableNode);
var properties = result.Type.GetProperties();
foreach (IProperty p in properties)
{
var attributes = p.Attributes;
bool isPrimaryKeyField = false;
bool isDbGenerated = false;
bool isColumn = false;
foreach (IAttribute attr in attributes)
{
if (attr.AttributeType.Name == "Column")
{
isColumn = true;
foreach (var arg in attr.NamedArguments) // NamedArguments contains no items.
{
if (arg.Key.Name == "IsPrimaryKey")
{
isPrimaryKeyField = (bool)arg.Value.ConstantValue == true;
}
if (arg.Key.Name == "IsDbGenerated")
{
isDbGenerated = (bool)arg.Value.ConstantValue == true;
}
}
}
}
if (isColumn)
{
... // Create a parameter as appropriate.
}
}
}
This all works until I try to loop through the IAttribute.NamedArguments because the collection contains no elements. However, when I go through the debugger and examine the value of 'attr', I can see that there is a private variable called 'unresolved', which contains a list of all the arguments I want but I can find no way to access this through code.
How do I get at the contents of this 'unresolved' variable? Do I need to do something more with the Resolver? This is my first time using NRefactory so I'm not overly familiar with all the nuances yet. I've been having a tough time finding an example that goes into this level of depth on Google and the documentation I've seen for NRefactory doesn't seem to cover it. Any help would be appreciated.
I figured it out. I needed to load the assembly for System.Data.Linq into the IProjectContent before resolving the SyntaxTree.
...
CecilLoader loader = new CecilLoader();
Assembly[] assembliesToLoad = {
...
typeof(System.Data.Linq.Mapping.ColumnAttribute).Assembly
...};
IUnresolvedAssembly[] projectAssemblies = new IUnresolvedAssembly[assembliesToLoad.Length];
for(int i = 0; i < assembliesToLoad.Length; i++)
{
projectAssemblies[i] = loader.LoadAssemblyFile(assembliesToLoad[i].Location);
}
IProjectContent project = new CSharpProjectContent();
project = project.AddAssemblyReferences(projectAssemblies);
...

AccessViolationException when accessing the Above, Below, Suffix, and Prefix properties of a Dimension

In Revit 2013 I have tool that I'm making that copies dimensions from one drafting view to another. I've got it to properly create a new version of a dimension including the Curve, DimensionType, and References but I'm having trouble with the properties Above, Below, Prefix, and Suffix. They copy just fine if at least one of them has a value. However, if none of them have a value then it will throw an AccessViolationException when I try to access them. I have tried to catch that exception but it bubbles up and it crashes Revit (I'm assuming it's caused by native code that fails).
How can I check to see if these properties have any value when I do my copying without triggering this AccessViolationException?
Autodesk Discussion Group Question
The DimensionData class is my own used for storing the dimension information so that it can be used to create the dimension in a separate document.
private IEnumerable<DimensionData> GetDimensionDataSet(Document document,
View view)
{
if (document == null)
throw new ArgumentNullException("document");
if (view == null)
throw new ArgumentNullException("view");
List<DimensionData> dimensionDataSet = new List<DimensionData>();
FilteredElementCollector dimensionCollector =
new FilteredElementCollector(document, view.Id);
dimensionCollector.OfClass(typeof(Dimension));
foreach (Dimension oldDimension in dimensionCollector)
{
Line oldDimensionLine = (Line)oldDimension.Curve;
string dimensionTypeName = oldDimension.DimensionType.Name;
List<ElementId> oldReferences = new List<ElementId>();
foreach (Reference oldReference in oldDimension.References)
oldReferences.Add(oldReference.ElementId);
DimensionData dimensionData;
try
{
string prefix = oldDimension.Prefix;
dimensionData = new DimensionData(oldDimensionLine,
oldReferences,
dimensionTypeName,
prefix,
oldDimension.Suffix,
oldDimension.Above,
oldDimension.Below);
}
catch (AccessViolationException)
{
dimensionData = new DimensionData(oldDimensionLine,
oldReferences, dimensionTypeName);
}
dimensionDataSet.Add(dimensionData);
}
return dimensionDataSet;
}
Regarding transactions: As far as I'm aware, you are only required to be inside a transaction when you are making any sort of CHANGE (modifications, deletions, additions). If all you are doing is collecting dimension information, you would not need a transaction, but when you use that information to create new dimensions in another document, that code would have to be inside a transaction. I have had a number of programs under development which did not yet modify the document but simply collected parameter settings and posted them to a TaskDialog.Show(). These programs worked fine, and I don't see anything in your code that actually modifies your model, so that doesn't seem to be your issue.
It seems like I bug.
Can you post the issue to the ADN Support?
The solution I can suggest is to use Parameters of the Dimension element instead of Dimension class properties.
For example, you can get Suffix and Prefix by following code
var suffixParameter =
oldDimension.get_Parameter(BuiltInParameter.SPOT_SLOPE_SUFFIX);
string suffix = null;
if (suffixParameter != null)
{
suffix = suffixParameter.AsString();
}
var prefixParameter =
oldDimension.get_Parameter(BuiltInParameter.SPOT_SLOPE_PREFIX);
string prefix = null;
if (prefixParameter != null)
{
prefix = prefixParameter.AsString();
}
Unfortunatelly, I don't tell you how to get Above and Below Properties via parameters, because I don't have a project to test. But you can easily determine parameters using BuiltInParameter Checker
Hope it helps.

Process taking lots of time in foreach loop in C#

I am using C# code and Tridion(CMS) classes to fetch data from Tridion, below is the code to get all the publications List from Tridion.
protected void btnPublishPublicationList_Click(object sender, EventArgs e)
{
try
{
PublicationBL pubBL = new PublicationBL();
TridionCollection<Publication> pubAllList = pubBL.getAllPublicationList();
List<PublicationsBO> pubBos = new List<PublicationsBO>();
foreach (Publication pub in pubAllList)
{
if ((pub.Title.StartsWith("07"))||(pub.Title.StartsWith("08")))
{
PublicationsBO pubBO = new PublicationsBO();
pubBO.publicationID = pub.ID;
pubBO.publicationName = pub.Title;
pubBos.Add(pubBO);
}
}
pubBL.createPublicationListXML(pubBos);
}
catch (Exception ex)
{
log.Error(ex.Message);
}
}
In above code on the button click, I am using .net code and using Tridion class to get all the publications List as below:
TridionCollection<Publication> pubAllList = pubBL.getAllPublicationList();
I am getting my all the publications list very fast from the Tridion, however when I am going for foreach loop as below my process gets stuck and it takes lots of time to do this.
foreach (Publication pub in pubAllList)
{
if ((pub.Title.StartsWith("07"))||(pub.Title.StartsWith("08")))
{
PublicationsBO pubBO = new PublicationsBO();
pubBO.publicationID = pub.ID;
pubBO.publicationName = pub.Title;
pubBos.Add(pubBO);
}
}
After debugging I found that when debugger comes to foreach (Publication pub in pubAllList) it is taking lots of time. I think while making the Publication class object is taking time and it is Tridion class.
Please suggest any other way to do this or suggest what is wrong in above code.
Thanks.
It is indeed because of Tridion's lazy-loading. If all you need is a list of Publication IDs and Titles, I'd recommend using:
TDSE tdse = new TDSEClass():
XmlDocument publicationList = new XmlDocument();
publicationList.LoadXml(tdse.GetListPublications(ListColumnFilter.XMLListIDAndTitle));
This will give you an XML document containing a list of all publications (/tcm:ListPublications/tcm:Item), and each Item will contain a Title and ID attributes.
If you need more detail than just ID and Title, then you will have to individually load each publication, which you can do by using the ID attribute tdse.GetObject().
Hope this helps.
N
according to this which I think is the developers site... it looks like getAllPublicationList is using some sort of lazy loading so even though you have the collection you don't really have the items in it.
It appears that you can set filters on their collection rather than after the fact so that it will only load the records you are interested in.
to clarify, lazy loading means that when the collection is returned the data needed to populate the items in it are not yet laded. It isn't until you access the item in the collection that the data (e.g. by creating a Publication item) for that item is actually loaded. The purpose for using a lazy collection is to allow filtering on the collection so that unnecessary expensive loads can be avoided.
HTH

Categories