Add reference when creating node Sensenet

I'm working with 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

Answers (2)

Thane Plummer
Thane Plummer

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.

  1. It can either point to any node, or
  2. It can be constrained to be a particular type, as specified in the CTD.

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

Anh Lam
Anh Lam

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

Related Questions