0

I have a Task list, with some custom fields. One of the fields is a hyperlink field which contains a link to a document set that is related to the task. The task is created via the server object model and the metadata is populated in a standard way.

SPListItem newTask = taskList.Items.Add();
SPFieldUrlValue link = new SPFieldUrlValue();
link.Url = docSetURL;
link.Description = docSetName;
newTask["RelatedLink"] = link;
newTask.Update();

When the the task is created, the URL of the hyperlink field doesn't appear to be URL encoded. It is displayed in the edit item modal as http://mysite/mylib/firstName lastName (12345)

The document sets I link to undergo name updates (which changes the underlying folder name). When the name of the document set is updated, the hyperlink field's URL automatically changes to reflect the new document set URL. This is something SharePoint is doing behind the scenes, not custom logic. This rename encodes the URL, so I am left with http://mysite/mylib/firstName%20lastName%20(12345)

I need this URL to be saved in non-encoded form. I'd love to explain the reasons, but it would cause undue mental hardship to all reading this question. When I try to update the field with the non-encoded version of URL the change doesn't stick, it remains encoded.

var newDocSetHyperlinkField = new SPFieldUrlValue();
newDocSetHyperlinkField.Url = @"http://mysite/mylib/firstName lastName (12345)";
newDocSetHyperlinkField.Description = "Doc Set Name";
task["RelatedLink"] = newDocSetHyperlinkField;
task.Update()

I've tested updating the task with PowerShell and it saves the non-encoded URL as expected. Why I am unable to update this field with a non-encoded URL using server object model?

  • The issue may be something else. Above code works perfectly for me in server object model. – Nadeem Yousuf Aug 20 '13 at 16:40
  • I think it has something to do with the automatic URL rewrite that happens when document set is renamed. I don't know enough about that interaction to venture a guess as to why its preventing the URL from being overwritten with the non-encoded version. – mbreske Aug 20 '13 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.