Custom Field does not save - c#

I've created a list (sharepoint 2010) that contains a cumstom field. The custom field inherits from SPFieldUser.
After I created a new ListItem (webfrontend) and have a look on the elements details the field is empty.
Webfronted NewItemForm
Webfronted ItemDetailsForm
Webfronted EditItemForm
My Code:
public class Vertreter_FieldType:SPFieldUser
{
public Vertreter_FieldType(SPFieldCollection fields, string fieldName)
: base(fields, fieldName)
{
}
public Vertreter_FieldType(SPFieldCollection fields, string typeName, string fieldName)
: base(fields, typeName, fieldName)
{
}
public override object GetFieldValue(string value)
{
return base.GetFieldValue(value);
}
public override string DefaultValue
{
get
{
return base.DefaultValue;
}
set
{
base.DefaultValue = value;
}
}
}
XML-fldtypes:
<FieldTypes>
<FieldType>
<Field Name="TypeName">Vertreter_FieldType</Field>
<Field Name="ParentType">User</Field>
<Field Name="TypeDisplayName">Vertreter</Field>
<Field Name="TypeShortDescription">Auswahl Vertreter</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="FieldTypeClass">CustomFieldTypes.Vertreter_FieldType, $SharePoint.Project.AssemblyFullName$</Field>
</FieldType>
</FieldTypes>
My Contenttype the List bases on:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
...
<Field ID="{2C338272-3BC8-45bc-B33E-5FBD1223F398}"
DisplayName="Administrator" Name="Administrator"
Type="Admin_FieldType" Required="TRUE"
UnlimitedLengthInDocumentLibrary="FALSE"
/>
<Field ID="{585CE72A-72D7-4ecc-8324-484BA1E483F1}"
DisplayName="Vertreter" Name="Vertreter"
Type="Vertreter_FieldType" Required="TRUE"
UnlimitedLengthInDocumentLibrary="FALSE"
>
</Field>
...
<FieldRefs>
...
<FieldRef ID="{2C338272-3BC8-45bc-B33E-5FBD1223F398}" Name="Administrator" Required="TRUE" ShowInNewForm="TRUE" ShowInEditForm="TRUE" />
<FieldRef ID="{585CE72A-72D7-4ecc-8324-484BA1E483F1}" Name="Vertreter" Required="TRUE" ShowInNewForm="TRUE" ShowInEditForm="TRUE" />
...
</FieldRefs>
</ContentType>
</Elements>
My environment: sharepoint 2010, visualstudio 2010
Thanks for your help!

Found the solution: I had to add List="UserInfo" to the FieldDefinition.
<Field ID="{585CE72A-72D7-4ecc-8324-484BA1E483F1}" DisplayName="Vertreter" Name="Vertreter" Type="Vertreter_FieldType" Required="TRUE" UnlimitedLengthInDocumentLibrary="FALSE" List="UserInfo">

Related

How to deserialize a nested Array from xml to c# object?

I'm trying deserialize the following soap xml response from my web service to a C# object:
<?xml version="1.0" encoding="utf-16"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body>
<ns2:ExportPositionsQueryResponse xmlns:ns2="http://ns" xmlns:ns3="http://nscpm/datatypes/schema/v2011_06_01" xmlns="http://nscpmexportvalue/schema/v2011_06_01">
<ns2:Positions>
<FieldTypes>
<FieldType ExtId="PF_NAME" Type="string"></FieldType>
<FieldType ExtId="POSITION_QUANTITY" Type="double"></FieldType>
<FieldType ExtId="INST_NAME" Type="string"></FieldType>
<FieldType ExtId="INST_TYPE" Type="string"></FieldType>
<FieldType ExtId="POSITION_TYPE" Type="string"></FieldType>
<FieldType ExtId="POSITION_CLASS" Type="string"></FieldType>
</FieldTypes>
<Position>
<Field ExtId="INST_NAME" Value="name"></Field>
<Field ExtId="INST_TYPE" Value="asd"></Field>
<Field ExtId="PF_NAME" Value="ads"></Field>
<Field ExtId="POSITION_CLASS" Value="asd"></Field>
<Field ExtId="POSITION_TYPE" Value="asd"></Field>
<Field ExtId="POSITION_QUANTITY" Value="asd"></Field>
</Position>
<Position>
<Field ExtId="INST_NAME" Value="asd"></Field>
<Field ExtId="INST_TYPE" Value="asd"></Field>
<Field ExtId="PF_NAME" Value="asd"></Field>
<Field ExtId="POSITION_CLASS" Value="ads"></Field>
<Field ExtId="POSITION_TYPE" Value="NORMAL"></Field>
<Field ExtId="POSITION_QUANTITY" Value="asd"></Field>
</Position>
<Position>
</ns2:Positions>
</ns2:ExportPositionsQueryResponse>
</soap:Body>
</soap:Envelope>
After deserialization, xmlSerializer creates an object only with element FieldTypes and the last element of Position. I think XmlSerializer has a problem with the nested Array(PositionField[][] Position) and can't deserialize all Position array elements. Here is part of my C# object class, which I'm using for deserialization:
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://ns")]
public partial class ExportPositionsQueryResponsePositions
{
private FieldTypesFieldType[] fieldTypesField;
private PositionField[][] positionField;
[System.Xml.Serialization.XmlArrayAttribute(Namespace = "http://ns")]
[System.Xml.Serialization.XmlArrayItemAttribute("FieldType", IsNullable = false)]
public FieldTypesFieldType[] FieldTypes
{
get
{
return this.fieldTypesField;
}
set
{
this.fieldTypesField = value;
}
}
[System.Xml.Serialization.XmlArray(ElementName ="Position",Namespace = "http://ns")]
[System.Xml.Serialization.XmlArrayItem("Field", typeof(PositionField[]), IsNullable = false)]
public PositionField[][] Position
{
get
{
return this.positionField;
}
set
{
this.positionField = value;
}
}
}
For creating this class, I've used the "Paste special" function of Visual Studio. But also with the svcutil tool I get same problem.
I've solved the problem. I should edit automatically generated proxy client class. I've replaced all nested arrays [][] with List<T> and serialization works again. here's example:
wrong generated part of code:
private PositionField[][] positionField;
public PositionField[][] Position
{
get
{
return this.positionField;
}
set
{
this.positionField = value;
}
}
edited part of code:
private List<PositionField> positionField;
public List<PositionField> Position
{
get
{
return this.positionField;
}
set
{
this.positionField = value;
}
}

How to get the value of a child element in xml

I am trying to edit a existing XML file through C# and need to identify the sections I want (block name="treeDeadTree01" and block name="treeDeadTree02") Then get the value of "count" in the child element "drop". I have been able to identify the elements (block name="treeDeadTree01" and block name="treeDeadTree02") but cant figure out how to get the value of "count" from the element "drop" of the specific elements (block name="treeDeadTree01" and block name="treeDeadTree02")
The XML file is a config file from the game 7 Days To Die and I am trying to make a application to more easily edit the configs of the game. After I find the value of "count" I am putting it into a DataGridView.
My question is, how after I find the block element I want do I find the value of "count" in the "drop" child element of the block element?
This is a section of the XML file I am trying to edit
<blocks>
<block name="treeDeadTree01">
<property name="Extends" value="treeMaster" />
<property name="Model" value="Entities/Trees/White_Oak22Prefab" />
<property name="ModelOffset" value="0,-0.3,0" />
<property name="MultiBlockDim" value="1,4,1" />
<property name="BigDecorationRadius" value="4" />
<property name="Collide" value="movement,melee,bullet,arrow,rocket" />
<drop event="Harvest" name="resourceWood" count="515" tag="oreWoodHarvest" />
<property name="ParticleOnDeath" value="treeGib_dead_01" />
<!-- <drop event="Destroy" name="treePlantedMaple1m" count="x"/> -->
<property name="SortOrder2" value="0140" />
<!-- SortTree -->
</block>
<block name="treeDeadTree02">
<!-- almost a shrub -->
<property name="Extends" value="treeMaster" />
<property name="IsTerrainDecoration" value="true" />
<property name="Model" value="Entities/Trees/Haunted_Shrub_WastelandPrefab" />
<property name="ModelOffset" value="0,-0.3,0" />
<property name="Collide" value="melee,rocket" />
<!-- no movement effect on Shape="ModelTree" -->
<drop event="Harvest" name="resourceWood" count="215" tag="oreWoodHarvest" />
<property name="ParticleOnDeath" value="treeGib_dead_02" />
<!-- <drop event="Destroy" name="treePlantedMaple1m" count="x"/> -->
<property name="FilterTags" value="foutdoor,ftrees,fshrubbery" />
<property name="SortOrder1" value="a060" />
</block>
</blocks
My current code
string[] GatherRateNames = {"treeDeadTree01", "treeDeadTree02"};
string XMLBlocksPath = "F:\\SteamLibrary\\steamapps\\common\\7 Days To Die\\Data\\Config\\blocks.xml";
private void makeGatherRatesList()
{
XmlDocument doc = new XmlDocument();
doc.Load(XMLBlocksPath);
XmlNodeList aNodes = doc.SelectNodes("/blocks/block");
foreach (XmlNode aNode in aNodes)
{
XmlAttribute idAttribute = aNode.Attributes["name"];
if (GatherRateNames.Contains(idAttribute.ToString()))
{
addItemToList(idAttribute.ToString(),"Value of count");
}
}
}
private void addItemToList(string itemName, int itemValue)
{
}
In the end I want to get the name of the block like "treeDeadTree01" then get the value of "count" inside the element "drop" of that block and put both of those in a DataGridView and be able to edit the value of count through the DataGridView then repeat that with every block I need.
Using Xml Linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Globalization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
CultureInfo culture = (CultureInfo)CultureInfo.InvariantCulture.Clone();
culture.NumberFormat.NumberDecimalSeparator = ",";
culture.NumberFormat.NumberGroupSeparator = ".";
XmlReader reader = XmlReader.Create(FILENAME);
List<Block> blocks = new List<Block>();
while (!reader.EOF)
{
if (reader.Name != "block")
{
reader.ReadToFollowing("block");
}
if (!reader.EOF)
{
Block newBlock = new Block();
blocks.Add(newBlock);
XElement xBlock = (XElement)XElement.ReadFrom(reader);
newBlock.name = (string)xBlock.Attribute("name");
XElement drop = xBlock.Element("drop");
if (drop != null)
{
object count = drop.Attribute("count");
newBlock.count = (count == null)? null : (decimal?)decimal.Parse((string)count, culture);
}
}
}
}
public class Block
{
public string name { get; set; }
public decimal? count { get; set; }
}
}
}

c# Use NLog with Mongo

i would log my data with NLog and MongoDB. I configured my NLog.config like this:
<extensions>
<add assembly="NLog.Mongo"/>
</extensions>
...
<target xsi:type="Mongo"
name="mongo"
includeDefaults="false"
connectionString="mongodb://localhost"
collectionName="myCollection"
databaseName="logs"
cappedCollectionSize="26214400">
<field name="_id" layout="${Id}"/>
<field name="ts" layout="${Timestamp}" bsonType="DateTime"/>
<field name="cap" layout="${ApplicationCaller}" />
</target>
But how can i pass value ID, Timestamp, ApplicationCaller in my log??
I try this code:
public void LogExceptionOnMongo(string callIdentifier, string applicationCaller)
{
var _myLogger = LogManager.GetLogger("mongo");
var logEventInfo = new LogEventInfo(LogLevel.Fatal, "", "Exception");
logEventInfo.Properties["CallIdentifier"] = callIdentifier;
logEventInfo.Properties["TimeStamp"] = DateTime.UtcNow;
logEventInfo.Properties["ApplicationCaller"] = applicationCaller;
_myLogger.Log(logEventInfo);
}
and this configuration:
<target xsi:type="Mongo"
name="mongo"
includeDefaults="false"
connectionString="mongodb://localhost"
collectionName="myCollection"
databaseName="logs"
cappedCollectionSize="26214400">
<field name="_id" layout="${event-properties:item=CallIdentifier}"/>
<field name="ts" layout="${event-properties:item=TimeStamp}" bsonType="DateTimeUtc"/>
<field name="cap" layout="${event-properties:item=ApplicationCaller}" />
</target>
But i obtain this log in mongo (I don't wont circled data):
Please can anyone help me?? Thank you and sorry for my English
There would have to be a code change to accommodate this. See Issue for more information.
New property includeEventProperties can now be configured:
<target xsi:type="Mongo"
name="mongo"
includeDefaults="false"
includeEventProperties="false"
connectionString="mongodb://localhost"
collectionName="myCollection"
databaseName="logs"
cappedCollectionSize="26214400">
<field name="_id" layout="${event-properties:item=CallIdentifier}"/>
<field name="ts" layout="${event-properties:item=TimeStamp}" bsonType="DateTimeUtc"/>
<field name="cap" layout="${event-properties:item=ApplicationCaller}" />
</target>
Just use ver. 4.6.0.68 from nuget: https://www.nuget.org/packages/NLog.Mongo
This morning i have tried use the new version of library, this is the new configuration:
<target xsi:type="Mongo"
name="mongo"
includeDefaults="false"
connectionString="mongodb://localhost"
collectionName="myCollection"
databaseName="logs"
cappedCollectionSize="26214400"
includeEventProperties="false">
<field name="_id" layout="${event-properties:item=CallIdentifier}"/>
<field name="ts" layout="${event-properties:item=TimeStamp}" bsonType="DateTime"/>
<field name="cap" layout="${event-properties:item=ApplicationCaller}" />
</target>
with new tag includeEventProperties=false and with its new property in the class MongoTarget and it works fine. This is the result:
Thank you so much, great job

Register XMPP account using Matrix gives error 400

here is my code
regXmppClient.SetUsername("abcd");
regXmppClient.SetXmppDomain("abcd.com");
regXmppClient.Password = "abcd";
regXmppClient.RegisterNewAccount = true;
regXmppClient.Open();
regXmppClient.OnRegister += new EventHandler<Matrix.EventArgs>(xmppCon_OnRegister);
regXmppClient.OnRegisterInformation +=
new EventHandler<Matrix.Xmpp.Register.RegisterEventArgs>(xmppCon_OnRegisterInformation);
regXmppClient.OnRegisterError += new EventHandler<Matrix.Xmpp.Client.IqEventArgs>(xmppCon_OnRegisterError);
XMl Error log
<iq type="error" id="MX_2" from="abcd.com" to="abcd.com/55ce2afc" xmlns="jabber:client">
<query xmlns="jabber:iq:register">
<username>abcd</username>
<password>abcd</password>
<email />
<name />
<x xmlns="jabber:x:data" type="form">
<title>XMPP Client Registration</title>
<instructions>Please provide the following information</instructions>
<field var="FORM_TYPE" type="hidden">
<value>jabber:iq:register</value>
</field>
<field var="username" type="text-single" label="Username">
<required />
</field>
<field var="name" type="text-single" label="Full name" />
<field var="email" type="text-single" label="Email" />
<field var="password" type="text-private" label="Password">
<required />
</field>
</x>
</query>
<error code="400" type="modify">
<jid-malformed xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
</error>
</iq>
I got solution finally, I can Register new account using XmppClientConnection
_xmppConnection.Server = SERVER_NAME;
_xmppConnection.ConnectServer = SERVER_NAME;
_xmppConnection.Username = objxmppData.UserName;
_xmppConnection.Password = objxmppData.password;
**_xmppConnection.RegisterAccount = true;**
_xmppConnection.Open();
_xmppConnection.OnAuthError += loginFailed;
_xmppConnection.OnLogin += new ObjectHandler(xmpp_OnLogin);
As in above code make RegisterAccount = true will do both create new account and loggedin with the same.
you code submits a xdata from and the old jabber style fields , , and . Some servers have problems with that, and I guess they are right.
If you want to use old jabber style registration then remove the xdata from in your set query. If you want to use xdata then remove the old jabber style fields.
see also: http://forum.ag-software.net/thread/874-How-to-Register-a-new-Account-on-an-xmpp-server

How can I iterate on XML elements from an XML using XMLReader and bypass whitespaces?

Let say you have an XML like this:
<?xml version="1.0" encoding="utf-8"?>
<Class HashCode="307960707">
<Person>
<Class HashCode="-2020100801">
<FullName>
<FirstName>Dan</FirstName>
<LastName>K</LastName>
</FullName>
</Class>
<Age>20</Age>
<Class HashCode="-439631396">
<Address>
<Street>abc</Street>
<City>new york</City>
<ZipCode>30500</ZipCode>
<PhoneNumber>1245</PhoneNumber>
</Address>
</Class>
<Class HashCode="-1436395737">
<Person>
<Class HashCode="-1303968324">
<FullName>
<FirstName>katty</FirstName>
<LastName>G</LastName>
</FullName>
</Class>
<Age>18</Age>
<Class HashCode="-439631396">
<Address />
</Class>
<Class HashCode="307960707">
<Person />
</Class>
</Person>
</Class>
I want to be able to iterate only elements with XMLReader in the order they appear, which means class->Person-> class->FullName ,etc..
I was trying to navigate with methods like XMLReader.ReadStartElement() and it didn't work especially when I read a whitespaces like "\n" which appears to be an element also. :/
I was trying to bypass that whitespace with method XMLReader.Read() with no success.
Please help me understand how should I navigate that way.
XmlReader constructor has an overload that takes an XmlReaderSettings object. The XmlReaderSettings object has an IgnoreWhitespace property.
In order to read only the next elements you can implement an extension method on XmlReader.
Here's an example:
public static class ExtensionMethods
{
public static bool ReadNextElement(this XmlReader reader)
{
while (reader.Read())
if (reader.NodeType == XmlNodeType.Element)
return true;
return false;
}
}
And here's a little console application that will demonstrate this:
public class Program
{
public static void Main(string[] args)
{
var settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
settings.IgnoreProcessingInstructions = true;
var reader = XmlReader.Create("XMLFile1.xml", settings);
while (reader.ReadNextElement())
Console.WriteLine(reader.Name);
}
}

Categories

Resources