I am using EWS to develop my email client. I found that if I store ItemId in viewstate it will cause an exception says:
Type 'Microsoft.Exchange.WebServices.Data.ItemId' in Assembly 'Microsoft.Exchange.WebServices, Version=14.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.
If I store ItemId
as string like:
ViewState["itemId"] = id.ToString();
and then try to cast back,
ItemId id = (ItemId)ViewState["itemId"];
it says I can not convert from string to ItemId
. Any idea?
As the error message suggests, you can't store an object in viewstate unless it's marked as serializable.
Looking at the documentation here, it seems that the ItemId class has a UniqueId property, which is a string, and a constructor which takes a string 'uniqueId' parameter.
So, can you store the uniqueId in viewstate, and regenerate the object using the constructor?
You are storing string and expecting ItemId. You should store as
ItemId itemId = new ItemId();
ViewState["itemId"] = itemId;
However, since ItemId is not seriaziable, it cannot be stored. To store it make your serializable class inherited from ItemId and override all the members and store it in ViewState
[Serializable]
public class MyItemId: ItemId {
// override all properties
}
Store like this
MyItemId itemId = new MyItemId();
ViewState["itemId"] = itemId;
and retrieve
MyItemId id=(MyItemId)ViewState["itemId"];
Did you try:
ItemId id = new ItemId( ViewState["itemId"] );
http://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.itemid.itemid(EXCHG.80).aspx
You cannot store it in ViewState.
However you could probably store it in Session, since it uses the binary formatter.
ViewState is serialized with the LosFormatter class.