1

I want to update a field 'End Time' of type DateTime in my Sharepoint list. I want to update the field('End Time') with null.Whenever I try this I get an exception "Input string is not in correct format". beloq is my code:

  public static void DeleteEndTime(string resource, string digest, Cookie cookie)
    {
        try
        {
            var client = new RestClient();
            client.BaseUrl = new Uri(_baseUri);
            var request = new RestRequest();
            request.AddParameter(cookie.Name, cookie.Value, ParameterType.Cookie);
            request.AddHeader("Accept", "application/json");
            request.AddHeader("Content-Type", "application/json;odata=verbose");
            request.AddHeader("X-RequestDigest", digest);
            request.AddHeader("X-HTTP-Method", "MERGE");
            request.AddHeader("IF-MATCH", "*");
            request.Resource = resource;
            request.RequestFormat = DataFormat.Json;
            request.Method = Method.POST;
            string jsonbody = @"{ '__metadata':{ 'type':'SP.Data.TimesheetListItem'},'End_x0020_Time':'"+ (DateTime?)null +"'}";
            request.AddParameter("Application/Json;odata=verbose", jsonbody, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);

        }
        catch (Exception ex)
        {
            //TODO - Log
        }
    }

1 Answer 1

2

Your request body should be like following.

string jsonbody = @"{ '__metadata':{ 'type':'SP.Data.TimesheetListItem'},'End_x0020_Time':null}"

So try it.

'End_x0020_Time':'"+ (DateTime?)null +"' is not valid. For null values, casting is not required.

3
  • Was about to answer that you don't have to cast the null to Datetime :)
    – Gintas K
    Commented Nov 3, 2016 at 12:21
  • I'm getting the following exception when I try this "Cannot convert a primitive value to the expected type 'Edm.DateTime'. See the inner exception for more details" @Atish Dipongkor
    – Dharma Raj
    Commented Nov 3, 2016 at 12:36
  • you saved my time, But I dont understand the difference string jsonbody = @"{ '__metadata':{ 'type':'SP.Data.TimesheetListItem'},'End_x0020_Time':null} and @"{ '__metadata':{ 'type':'SP.Data.TimesheetListItem'},'End_x0020_Time':'"+ null +"'}";
    – Dharma Raj
    Commented Nov 3, 2016 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.