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 tried both default credential and user/pass but keep getting 401 unauthorized error. What's the fix?

Here is the error I am getting ....

New-WebServiceProxy : The request failed with HTTP status 401: Unauthorized.
At C:\Scripts\GetItemID_UsingWebServices.ps1:1 char:26
+ $WS = New-WebServiceProxy <<<<  -uri http://server/sites/promo/_vti_bin/SiteData.asmx
    + CategoryInfo          : ObjectNotFound: (http://server...n/SiteData.asmx:Uri) [New-WebServiceProxy], WebExcept
   ion
    + FullyQualifiedErrorId : WebException,Microsoft.PowerShell.Commands.NewWebServiceProxy
Property 'Credentials' cannot be found on this object; make sure it exists and is settable.
At C:\Scripts\GetItemID_UsingWebServices.ps1:2 char:5
+ $WS. <<<< Credentials = [System.Net.CredentialCache]::DefaultCredentials
    + CategoryInfo          : InvalidOperation: (Credentials:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At C:\Scripts\GetItemID_UsingWebServices.ps1:12 char:26
+ $GUS = $WS.GetURLSegments <<<< ($ItemURL, $webID, $bucketID, $strListID, $strItemID)
    + CategoryInfo          : InvalidOperation: (GetURLSegments:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Code:

$WS = New-WebServiceProxy -uri http://server/_vti_bin/SiteData.asmx
$WS.Credentials = [System.Net.CredentialCache]::DefaultCredentials
#$WS.Credentials = [System.Net.NetworkCredential]::("user", (ConvertTo-SecureString "password" -AsPlainText -force), "domain")
$ItemURL = "http://server/sites/promo/sales/testLib/TestFile.txt"
$GUS = $WS.GetURLSegments($ItemURL, $webID, $bID, $strListID, $strItemID)
Write-Host $strItemID

Update with new code!

This code is working (meaning at least no more 401 error). However, I am still working on it since it does not return any values for webid or bid or listid or itemid.

$webID = $null
$bID = $null
$strListID = $null
$strItemID = $null
$GUS
$uri = "http://Server/sites/Promo/TestDocLibrary44/_vti_bin/SiteData.asmx"
$WS = New-WebServiceProxy -uri $uri -UseDefaultCredential
$ItemURL = "http://Server/sites/Promot/TestDocLibrary44/2011102100006b.pdf"
$GUS = $WS.GetURLSegments($ItemURL, [REF]$webID,  [REF]$bID,  [REF]$strListID,  [REF]$strItemID)
write-Host $strItemID.value # returning null or blank
write-Host $webID # returning null or blank
Write-Host "Test "
share|improve this question
    
Where are all the SP Gurus ?????? –  Mary Rivers Oct 24 '11 at 22:40
    
I went ahead and edited your question to include the information you gave in an answer below. You might also consider asking a new question since we have solved your 401 Unauthorized error. –  Kit Menke Oct 27 '11 at 0:25
add comment

3 Answers

If you're going to specify the password, I believe it has to be a secure string:

$WS.Credentials = [System.Net.NetworkCredential]::("USERNAME", (ConvertTo-SecureString "PASSWORD" -AsPlainText -force))

If you're specifying a domain, I believe it needs to be as a parameter, not as a fully qualified username:

$WS.Credentials = [System.Net.NetworkCredential]::("USERNAME", (ConvertTo-SecureString "PASSWORD" -AsPlainText -force), "DOMAIN")

See MSDN: http://msdn.microsoft.com/en-us/library/system.net.networkcredential.aspx

share|improve this answer
    
I updated the original post. I am still getting the same 401 unauthorized error. Is there something I need to check in IIS? From browser I can see all the methods for the _vti_bin/sitedata.asmx. I am logged in spadmin on the SP WFE. –  Mary Rivers Oct 25 '11 at 19:15
    
What are you trying to do would be a good place to start I guess... there's no a lot to go on here. –  webdes03 Oct 25 '11 at 20:31
    
I am trying to get doc id for an item using GetURLSegments method via web services with Powershell. –  Mary Rivers Oct 26 '11 at 13:05
add comment

I don't really know powershell but I know webservices... and I think you need to use DefaultNetworkCredentials instead of DefaultCredentials:

$WS = New-WebServiceProxy -uri http://server/sites/promo/sales/_vti_bin/SiteData.asmx
$WS.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$ItemURL = "http://server/sites/promo/sales/testLib/TestFile.txt"
$GUS = $WS.GetURLSegments($ItemURL, $webID, $bID, $strListID, $strItemID)
Write-Host $strItemID

Also, make sure the account you are running this script as has access to the SharePoint site. Some webservices require higher permissions levels as well.

share|improve this answer
    
I copied and pasted your code and still 401 error... –  Mary Rivers Oct 26 '11 at 14:40
    
Have you double checked the account's permissions? –  Kit Menke Oct 26 '11 at 14:41
    
Yes, I am running this on WFE with SPAdmin who has full access to the site collection. –  Mary Rivers Oct 26 '11 at 15:13
    
I just noticed that your web service proxy URL was pointing to the root site. I'm guessing http://server/sites/promo/sales/ is the site which has your doclib in it. Try the updated code. –  Kit Menke Oct 26 '11 at 15:19
    
Yes sir. I have tried that combination as well. I tried $WS = New-WebServiceProxy -uri $uri -UseDefaultCredential then I get following error and dont receive 401 error. Argument: '2' should be a System.Management.Automation.PSReference. Use [ref]. At line:5 char:26 + $GUS = $WS.GetURLSegments <<<< ($ItemURL, $webID, $bID, $strListID, $strItemID) –  Mary Rivers Oct 26 '11 at 15:43
show 3 more comments

include domain if necessary

$username = "username"
$password = "password"

$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))

assign $cred to the webServicesProxy.Credentials

$webServicesProxy.Credentials = $cred

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.