JXJ

If I am getting list items via the Sharepoint API, how can I determine if an item field has been set up as "date and time" or "date only"

If I have:

SPList list;

I don't see where in list.Fields[fieldname] I can find that information.



Re: SharePoint - Development and Programming Determining "Date & Time" vs. "Date Only" for a list item

smc750

Code Snippet

SPFieldDateTime fld = (SPFieldDateTime)item.Fields["reqdate"];


if(fld.DisplayFormat == SPDateTimeFieldFormatType.DateOnly)
{
//whatever
}






Re: SharePoint - Development and Programming Determining "Date & Time" vs. "Date Only" for a list item

JXJ

Thanks - that *almost* gets me there....

My problem now is that the field is a calculated field.

So item.Fields["fldname"].Type == SPFieldType.Calculated, and the cast to a SPFieldDateTime fails. (Probably because the field actual contains "datetime;MM/DD/YYYY HH:MMTongue TiedS" instead of just the date and time.)

Is there a way to tell if a field that is defined as SPFieldType.Calculated is a date field, and what the format is (date only or date/time)

I am trying not to hard code field names so I need to determine the type at runtime, and then determine the output format based on the type.





Re: SharePoint - Development and Programming Determining "Date & Time" vs. "Date Only" for a list item

smc750

Code Snippet

SPFieldCalculated fld = (SPFieldCalculated)item.Fields["reqdate"];


if(fld.DateFormat == SPDateTimeFieldFormatType.DateOnly)
{
//whatever
}






Re: SharePoint - Development and Programming Determining "Date & Time" vs. "Date Only" for a list item

smc750

also if need to see what kind of calculated field then check the OutPutType property for SPFieldType.DateTime






Re: SharePoint - Development and Programming Determining "Date & Time" vs. "Date Only" for a list item

JXJ

Thanks - I appreciate the help.

I found the SPFieldCalculated right after I posted the last question.

Now there is one last issue - I've tried several things and I can't seem to find how you get the current calculated value of the item. Do you need to strip out the datatype embedded in the value first

For example, my calculated field is a date. If I use:

item["fieldname"].ToString()

it returns the string "datetime;#2007-08-22 00:00:00", which is the calculated date, but has the data type embedded in front of it.

I'm trying to access the data through a view, so basically this is what I am doing:

Code Snippet

SPSite siteCollection = new SPSite(server);

SPWeb site = siteCollection.OpenWeb(siteName);

SPList list = site.Lists[listname];

SPView listView = list.Views[viewName];

SPListItemCollection viewItems = list.GetItems(listView);

So now "viewItems" has the data I want.

Then I do something like:

Code Snippet

foreach (SPListItem item in viewItems)

{

if (item.Fields["myfield"].Type == SPFieldType.Calculated)

{

SPFieldCalculated cf = (SPFieldCalculated)item.Fields["myfield"];

Console.WriteLine("Value of myfield = " + cf.ToString(); // not right

}

}

But this does not output the value of "myfield" - instead it outputs "Value of myfield = myfield" - in other words, cf.ToString() is returning the name of the field, not the value.

I can't see where you get the actual calculated value out of SPFieldCalculated. I can get it directly out of item["myfield"], but then I need to strip out the "datetime;#" - that's OK, I can do that, but is it really necessary Doesn't SPFieldCalculated know how to do that and return the actual data





Re: SharePoint - Development and Programming Determining "Date & Time" vs. "Date Only" for a list item

smc750

SPFieldCalculated cf = (SPFieldCalculated)item.Fields["myfield"];

Console.WriteLine("Value of myfield = " + cf.GetFieldValueForEdit(item.Fields["myfield"]);






Re: SharePoint - Development and Programming Determining "Date & Time" vs. "Date Only" for a list item

JXJ

That looks like it should work... but it gives an exception - "Specified argument was out of the range of valid values"





Re: SharePoint - Development and Programming Determining "Date & Time" vs. "Date Only" for a list item

JXJ

I found the problem - don't use ".Fields".

Instead of:

Code Snippet

SPFieldCalculated cf = (SPFieldCalculated)item.Fields["myfield"];

Console.WriteLine("Value of myfield = " + cf.GetFieldValueForEdit(item.Fields["myfield"]);

I used:

Code Snippet

SPFieldCalculated cf = (SPFieldCalculated)item.Fields["myfield"];

Console.WriteLine("Value of myfield = " + cf.GetFieldValueForEdit(item["myfield"]);

Your help is much appreciated!!





Re: SharePoint - Development and Programming Determining "Date & Time" vs. "Date Only" for a list item

kjhjkh

hi Guys. I'm also batting with gettin a SPFieldDateTime value back. Its a custom datetime field I put in on the list item. This is in a webpart

Here is the code I used

SPFieldDateTime d = (SPFieldDateTime)item.Fields["Expiry Date"];

writer.Write(d.GetFieldValueForEdit(item.Fields["Expiry Date"]));

Id gives me error

ErrorTongue Tiedpecified cast is not valid.

Message: at Microsoft.SharePoint.SPFieldDateTime.GetFieldValueAsText(Object value)

at Microsoft.SharePoint.SPField.GetFieldValueForEdit(Object value)

at MoveBTDDocToArchive.MoveBTDDocToArchive.Render(HtmlTextWriter writer)

Please Help





Re: SharePoint - Development and Programming Determining "Date & Time" vs. "Date Only" for a list item

kjhjkh

I now tried

writer.Write(d.GetFieldValueForEdit(item["Expiry Date"]));

It returns nothing





Re: SharePoint - Development and Programming Determining "Date & Time" vs. "Date Only" for a list item

JXJ

Looking at it more closely, I don't know that you can cast to SPFieldDateTime (it's hard to say because the Sharepoint API is so poorly documented).

But I have found that in my case, I don't need it. To get the formatted date from a field, this works for me:

Code Snippet

if (list.Fields["Date Field"].Type == SPFieldType.DateTime)

string dateString = item.Fields["Date Field"].GetFieldValueForEdit(item["Date Field"])

If the field is set up as Date Only, this just returns the date. If it is Date & Time, it returns both.

But there is yet another problem....

On my system, my list column has "1/1/2007" stored as the value in the Date Only column, and when I look at the value of item["Date Field"], it has "1/1/2007 12:00:00 AM".

But the above code returns "12/31/2006" !!! (the day before)

It must be trying to do some adjustment for local time. Nothing is ever simple...





Re: SharePoint - Development and Programming Determining "Date & Time" vs. "Date Only" for a list item

kjhjkh

It works Smile. Thanks dude




Re: SharePoint - Development and Programming Determining "Date & Time" vs. "Date Only" for a list item

Krespo

Hi, guys. Do you khow how to modify field value
I can't understand whether GetFieldValueForEdit is method which is available for modifying the field value.




Re: SharePoint - Development and Programming Determining "Date & Time" vs. "Date Only" for a list item

Krespo

Hi, guys. Do you khow how to modify field value
I can't understand whether GetFieldValueForEdit is method, which is available for modifying the field value