Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to delete a field (site column bound to managed metadata) in a view with the following powershell script:

$spWeb = Get-SPWeb -identity http://servernamehere/docs/test/sitename
$spView = $spWeb.GetListFromUrl("/EMails/Forms/AllItems.aspx")
$spField = $spView.Fields["Document Author"]
$spView.ViewFields.Delete($spField["Document Author"])
$spView.Update()

I get this error on line2: v Exception calling "GetListFromUrl" with "1" argument(s): "Cannot complete this action.

Please try again." At line:1 char:32 + $spView = $spWeb.GetListFromUrl <<<< ("/EMails/Forms/AllItems.aspx") + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

What am I doing wrong?
Thanks

share|improve this question
add comment

4 Answers

You have several mistakes here:

  1. As djeeg mentioned, you need to use server relative url when calling GetListFromUrl, or (simplier) do not pass leading slash to GetListFromUrl, i.e. GetListFromUrl("Emails/Forms/AllItems.aspx"), I tested this code and it works for me.
  2. You are using GetListFromUrl, which returns SPList, but you assign it to $spView. So probably you have a mistake here.
  3. Fields indexer uses field internal name, which cannot contain white spaces, so you need to write smth like Fields["Document_x0020_Author"]
  4. You don't need to fetch the SPField, you can use it's name to delete from ViewFields, for example, $spView.ViewFields.Delete("Document_x0020_Author")

Whole script will be:

$spWeb = Get-SPWeb http://servernamehere/docs/test/sitename
$spList = $spWeb.GetListFromUrl("EMails/Forms/AllItems.aspx")
$spView = $spList.DefaultView
$spView.ViewFields.Delete("Document_x0020_Author")
$spView.Update()

This script worked for me without any errors.

share|improve this answer
    
+1 nice, i didn't look beyond the error message –  djeeg Apr 19 '11 at 7:36
    
:) thanks, djeeg! –  Andrey Markeev Apr 19 '11 at 8:28
add comment

A third alternative is to use the new TryGetList() method in SP2010:

$spWeb = Get-SPWeb http://servernamehere/docs/test/sitename$spList = $spWeb.GetListFromUrl("EMails/Forms/AllItems.aspx")
$list = $spWeb.Lists.TryGetList('my list title')
if ( $list )
{
    # do something intelligent here
}

This will get the list from title without any nullreference exceptions if the list isnt there.

More info here.

share|improve this answer
add comment

GetListFromUrl says to use GetListFromWebPartPageUrl

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.getlistfromurl.aspx

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.getlistfromwebpartpageurl.aspx

And the url probably needs to be server relative

SPList list = web.GetListFromWebPartPageUrl("/docs/test/sitename/EMails/Forms/AllItems.aspx");

Or have you tried $spWeb.Lists["EMails"]

http://msdn.microsoft.com/en-us/library/ms443550.aspx

share|improve this answer
add comment

Do you have any other webpart added to the AllItems.aspx page.. if so it will throw this error. can be checked by appending "?contents=1" to the url

btw..

  • you can use "/EMails/Forms/AllItems.aspx" or "http://servernamehere/docs/test/sitename/EMails/Forms/AllItems.aspx" as parameter to GetListFromUrl

  • you can use "Document Author", because 2010 accepts both displayname as well as internal name.. first it will try to resolve using internal name and then display name

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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