Reputation: 13
I'm working with sensenet and I have something confusing when I create a node in code behind on server side, I need to add a reference field to my node but I don't know how to do that.
I tried some thing like node["user"] = node1
but it doesn't work.
Upvotes: 1
Views: 63
Reputation: 10288
All Content (data) in Sensenet is structured as a binary tree where a Node refers to a particular Content object as specified in it's Content Type Definition (CTD). When a Node references another Node -- that is, it points to another location in the tree -- it can be one of two types.
If you correctly assign a Reference but get an error, it is likely that you are violating the type constraint in the CTD. See examples below.
CTD for reference Node of a particular type (partial)
<ContentType name="Agent" parentType="GenericContent" handler="Code.ContentHandlers.Agent" xmlns="http://schemas.sensenet.com/SenseNet/ContentRepository/ContentTypeDefinition">
<DisplayName>Agent</DisplayName>
<Icon>Content</Icon>
<Fields>
<Field name="Category" type="Reference">
<DisplayName>Agent Category</DisplayName>
<Description></Description>
<Configuration>
<AllowedTypes>
<Type>AgentCategory</Type>
</AllowedTypes>
<VisibleBrowse>Show</VisibleBrowse>
</Configuration>
</Field>
</Fields>
</ContentType>
Example C# code of assigning a Node to the Category Reference defined above.
var path = "/Root/PathToAgentCategory";
var agentCat = Node.LoadNode(path) as AgentCategory;
myAgentNode.Category = agentCat; // Syntax if you have a ContentHandler
myAgentNode["Category"] = agentCat; // Syntax for the GenericContent ContentHandler
Upvotes: 1
Reputation: 132
you should read it's document I find to add a reference field, you should use some thing like this
node.Addreferences("User", user1);
user1 is one node represent for a user that you need to reference n your field
Upvotes: 0