Add reference when creating node Sensenet - sensenet

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.

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.
It can either point to any node, or
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

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

Related

How do I create a complete record with inheritance type delegation?

example/models/example.py
class Example(models.Model)
_name = 'example.model'
product_tmpl_id = fields.Many2one('product.template','Product Template',delegate=True,ondelete='cascade',required=True)
example/models/product_template.py
class ProductTemplate(models.Model)
_inherit='product.template
example_ids = fields.One2many('example.model','product_tmpl_id',string='Item')
example/views/example.xml
<form>
<field name="product_tmpl_id" widget="many2one"/>
</form>
My understanding was that a product_template record would be automatically created with example, but this field is required and not letting me save a new record. When I perform an import of the example data adding these columns at the beginning for product_template ("exampleNN", "name", "type", "categ_id/id", "sale_ok", "purchase_ok",...) I get a matching product template with an id of "exampleNN_product_template" and identical name (though example does not have name so it must be using product template).
product_tmpl_id does not like being on the form view as it is required, yet not created yet with delegation inheritance. I used tree view instead to see product_tmpl_id. I was curious about its value after doing an import.

Edit XML tag by attribute

I have a XML document which looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<Data key="dailyKey">19283</Data>
</Configuration>
And in my vb.net program I want to change the value from "<Data>" by the attribute "dailyKey"
I have tried to understand myself on this but cannot figure out how to edit TAG by ATTRIBUTE
Please help, Richard
You could use XPath expressions and the SelectSingleNode method like this:
Dim node = xmlDoc.SelectSingleNode("//data[#key=""dailyKey""]")
Then you can modify the value of node as you wish to. You can find more XPath examples at MSDN.
The workflow in its entirety would be:
1. Load the XML Document for manipulation
You can use the XmlDocument class to load (and subsequently save) your XML Document like this:
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("<Here goes your url // You can also feed in a stream to this method>")
2. Locate the node you want to modify
As mentioned earlier, use the SelectSingleNode function to locate the node you are trying to modify the value of. It takes an XPath expression.
Dim node = xmlDoc.SelectSingleNode("//data[#key=""dailyKey""]")
3. Modify the node
You can now edit the node (tag) in whatever way you wish. It seems you want to edit the contained value. Do it by changing the Value property of the XmlNode:
node.Value = 224062 'Random value. Change to suit your needs.
4. Save the XML Document (Obviously :P)
xmlDoc.Save()

How can I pass a context (in a view, in a field tag) from another field's value?

I want to create a many2one field which, in view, passes a context:
<field name="my_m2o_field" context="{'foo': 'bar'}" />
The goal here is to affect the related view (i.e. When you click on "Create and Edit" in a dropdown, you get a popup rendered by the related object's current view).
Such field tag works as expected if, in context, I have something like "{'default_code': 'my.code'}", provided code field exists in the related object.
However, the context I actually need is too large (20 entries), and I have to generate 5 contexts like that (with a minor diference, since I have 5 similar fields).
I would like to wrap the context in a -non-storable- functional field (I'd need, actually, 5 similar functional fields), and pass such context as value for the context attribute:
<field name="my_context_field" invisible="1" />
<field name="my_m2o_field" context="my_context_field" />
Is it possible? What type should I use (type= argument in the function constructor).

When does SPMetal generate EntityRef properties for lookup fields?

I have a defined a content type called SPVideoDataItem containing those two fields:
<Field ID="{487F2AD6-D9D6-47AA-AA99-B3FFF893E689}" Name="LUVideoQuality" Group="Custom Columns" Type="Lookup" DisplayName="Video Quality" List="Lists/GlobalVideoQualityList" ShowField="Title" PrependId="TRUE" ShowInEditForm="TRUE" ShowInNewForm="TRUE"/>
<Field ID="{F348A825-764D-41EE-AF92-8CF1DC246E47}" Name="LUVideoTitle" Group="Custom Columns" Type="Lookup" DisplayName="Video Title" List="Lists/VideoItemList" ShowInEditForm="TRUE" ShowInNewForm="TRUE" ShowField="VideoItemTitle" PrependId="TRUE" Required="TRUE"/>
For LUVideoQuality I get a property in a class called VideoItemDataListSPVideoDataItem:
public SPVideoQualityItem VideoQuality
which exposes a EntityRef-Member, so I can access all properties of SPVideoQualityItem.
However, for LUVideoTitle spmetal just generates two properties in the parent class SPVideoDataItem
public string VideoTitleVideoItemTitle
public System.Nullable<int> VideoTitleId
where I can only access title and ID.
I wonder why spmetal handles this two lookups differently. Can anyone explain this behaviour? I would prefer that all lookups are handled like LUVideoQuality in my example.
I've encountered the same issue.
When I let go of the principle to create a separate content type and just created a list based on content type Item it did generate the entity refs.
Apparently it has something to do with the "WebId" property of the lookup fields. A colleague told me that they got it working by adding
WebId="~sitecollection"
to all lookup fields in the content type definitions (Elements.xml). Im not marking this as an answer because I did not check if it is working now because of this attribute or some other change in the definitions.

SharePoint 2010: RemoveFieldRef and Inherits="TRUE"

I have created a custom content type that inherits from the OOTB SharePoint Picture content type. The only customisations I have made is to add a simple URL field, and remove two of the fields on the base type. See below:
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- Parent ContentType: Picture (0x010102) -->
<ContentType ID="0x0101020027f16ab27e6e45a6848c25c47aaa7053"
Name="Custom Picture"
Description=""
Group="Custom"
Inherits="TRUE"
Version="0">
<FieldRefs>
<RemoveFieldRef ID="{b66e9b50-a28e-469b-b1a0-af0e45486874}" Name="Keywords" />
<RemoveFieldRef ID="{a5d2f824-bc53-422e-87fd-765939d863a5}" Name="ImageCreateDate" />
<FieldRef ID="{c29e077d-f466-4d8e-8bbe-72b66c5f205c}" Name="URL" DisplayName="URL" Required="FALSE" />
</FieldRefs>
</ContentType>
</Elements>
If I create a picture library based on my custom content type, the "URL" field that I added appears in the new/edit forms, however the two fields that I have attempted to remove are also displayed, i.e. the RemoveFieldRef's are being ignored. If I look at the content type in "Site Settings -> Content Type Gallery", these two fields are still listed there.
Setting Inherits="FALSE" on my custom content type (see MSDN definition) successfully removes just these two fields from the "Site Settings -> Content Type Gallery" page, however then none of the base fields are displayed in the new/edit forms -- only my custom "URL" field.
What can I do to ensure that all the fields from the base "Picture" content type are displayed on the new/edit forms of my picture library except the two fields that I have specifically removed?
I believe the nature of this issue to be the understanding of how content type inheritance works.
From MSDN (http://msdn.microsoft.com/en-us/library/aa544268.aspx)
If Inherits is TRUE, the child content type inherits all fields that are in the parent, >including fields that users have added.
If Inherits is FALSE or absent and the parent content type is a built-in type, the child >content type inherits only the fields that were in the parent content type when >SharePoint Foundation was installed. The child content type does not have any fields that >users have added to the parent content type.
If Inherits is FALSE or absent and the parent content type was provisioned by a sandboxed >solution, the child does not inherit any fields from the parent.
I think the key phrase above is "If Inherits is TRUE, the child content type inherits ALL fields that are in the parent including fields that users have added."
This means that in order to accomplish what you set out to do you will have inherits set to false and you will have to include FieldRef elements for all fields you wish to use in your content type.
You make no reference / don't include code for how the content type was added to your list instance. Make sure this has been updated to support the removal or setting of inherits to false.
These sites support what is described here.
http://kvdlinden.blogspot.com/2011/06/issues-with-removefieldref-and.html
http://nelsonlamprecht.wordpress.com/2010/08/25/sharepoint-2010-removefieldref-and-inherits%E2%80%9Dtrue%E2%80%9D/