1

I have a list "Orders" in SP, where I have some data. The list definition and instance were previously created in Visual Studio and then successfully deployed.

Today I've opened VS again and added new choice field "OrderType" to schema.xml file. Also I've added this field to all views. Now, I can see it on list view pages, I can successfully set this field from codebehind when I adding new item:

SPListItem item = SPContext.Current.List.AddItem();
item["Title"] = "MyNewTitle";
item["OrderType"] = "Choice1";
item.Update();

But, when I'm trying to access this field from Powershell, I get error:

$web = Get-SPWeb http://myweb
$list = $web.lists["Orders"]
$items = $list.items
$item = $items[0] # Item with filled fields
$item["OrderType"] # Returns nothing
$item["OrderType"] = "Choice1" # Returns error
$item.Update() 

Error:

Unable to index into an object of type Microsoft.SharePoint.SPListItem.
At line:1 char:7
+ $item[ <<<< "OrderType"] = "Choice1"
    + CategoryInfo          : InvalidOperation: (OrderType:String) [], RuntimeException
    + FullyQualifiedErrorId : CannotIndex

In fact, I even can't find the field on web:

$list.Fields.GetField("OrderType")

Error:

Exception calling "GetField" with "1" argument(s): "Column 'OrderType' does not exist. It may have been deleted by another user."
At line:1 char:22
+ $list.Fields.GetField <<<< ("OrderType")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

I have full access permissions for the item. I can still see this field and its data on my list view pages.

Where am I wrong?

2 Answers 2

1

You can find PowerShell samples here to get/set Choice fields: http://social.technet.microsoft.com/wiki/contents/articles/20831.sharepoint-a-complete-guide-to-getting-and-setting-fields-using-powershell.aspx#Set_and_Get_a_Choice_Field

For example, you should be able to set your field value like this:

$item["OrderType"] = $list.Fields["OrderType"].GetFieldValue("Choice1");            
$item.Update();
1
  • Unfortunately, $list.Fields["OrderType"] returns null. It looks like it doesn't exist, and I can't find it in $list.Fields | ft staticname output. The main two questions are "Where is my 'OrderType' field?" and "Why I can see it in list view if it doesn't exist?".
    – Alex
    Commented May 30, 2014 at 12:49
1

I found the solution, just simply opened my "problematic" field via "List" -> "Site settings" -> "OrderType", and than pressed "OK" (I didn't do any changes). Now I can access it via Powershell. Thanks to this post (it's about site columns but the solution is similar)!

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.