2016-05-04 25 views
0

.NET C#を使用してクライアントオブジェクトモデルに資格情報を渡そうとしています。私はShare Point 2013で働いており、Microsoft.SharePoint.Client.dllを使っています。Sharepoint 2013を使用して認証資格情報をクライアントオブジェクトモデルC#に渡すことができません。

私は多くの異なる繰り返しを試しましたが、私はまだ401 Unauthorizedエラーを受けています。ただし、ブラウザからサイトのURLにサインインすると、問題なくログインできるようになります。つまり、サイトにアクセスできます。以下は、私が使用した方法とコードの更新です。誰かが見てください、説明するか、これを正しく行う方法を私に示すことができれば、私は非常に感謝します! Authentication() -


認証()

public static ClientContext Authentication() 
    { 
     //Take 1: 
     NetworkCredential _myCredentials = new NetworkCredential(userNameFixed, passwordFixed,"https://CLARITYCON/"); 


     ClientContext clientContext = new ClientContext(siteURL); 

     clientContext.Credentials = _myCredentials; 

     return clientContext; 

     //Take 2: 
     //ClientContext clientContext = new ClientContext(siteURL); 


     //SecureString passWord = new SecureString(); 

     //foreach (char c in passwordFixed.ToCharArray()) passWord.AppendChar(c); 
     //clientContext.Credentials = new SharePointOnlineCredentials(userNameFixed, passWord); 

     //try 
     //{ 
     // Console.WriteLine("Working!"); 
     // return clientContext; 
     //} 
     //catch (Exception e) 
     //{ 
     // Console.WriteLine(e); 
     // return null; 
     //} 

     // Take 3: 
     //using (ClientContext clientContext = new ClientContext(siteURL)) 
     //{ 
     // SecureString passWord = new SecureString(); 

     // foreach (char c in passwordFixed.ToCharArray()) passWord.AppendChar(c); 

     // clientContext.Credentials = new SharePointOnlineCredentials(userNameFixed, passWord); 

     // try 
     // { 
     //  return clientContext; 
     // } 

     // catch (Exception e) 
     // { 
     //  Console.WriteLine(e); 
     //  return null; 
     // } 

     //} 


    } 

UPDATE

はまた、私は呼び出していメソッドが追加されました。

DeleteAFile()

/// <summary> 
    /// Will list out all the items within a Site, conduct a search and delete the item when found. 
    /// </summary> 
    /// <param name="sFileName"></param> 
    /// <param name="sFldrLoc"></param> 
    private static void DeleteAFile(string sFileName, string sFldrLoc) 
    { 

     var clientContext = Authentication(); 

     Web web = clientContext.Web; 
     ListCollection collList = web.Lists; 

     List oList = collList.GetByTitle(sFldrLoc); 

     CamlQuery query = new CamlQuery(); 

     query.ViewXml = "<View><Query><Where><Leq>" + 
      "<FieldRef Name='ID'/><Value Type='Number'>100</Value>" + 
      "</Leq></Where></Query><RowLimit>50</RowLimit></View>"; 

     ListItemCollection collListItem = oList.GetItems(query); 

     clientContext.Load(collListItem, 
      items => items.IncludeWithDefaultProperties(
       item => item.DisplayName)); 
     clientContext.ExecuteQuery(); 


     foreach (ListItem listitem in collListItem) 
     { 
      if (listitem.DisplayName.Equals(sFileName)) 
      { 
       listitem.DeleteObject(); 
       clientContext.ExecuteQuery(); 

       Console.WriteLine("{0}, has been deleted sucessfully!", listitem.DisplayName); 
      } 

     } 
    } 

コメントしたコードは、私が実装するのに疲れていたコードのすべてのバージョンです。すべてが401 Unauthorizedエラーを返しています。 "CLARITYCONは" あなたのドメインは、あなたのSharepointのURLでない場合

答えて

0

あなたはほとんどが最後のパラメータではありませんあなたのSharepointのURL

NetworkCredential _myCredentials = new NetworkCredential(userNameFixed, passwordFixed,"https://CLARITYCON/"); 

であなたのドメインを渡すあなたの最もこのコードのようにHTTPSを削除:

NetworkCredential _myCredentials = new NetworkCredential(userNameFixed, passwordFixed,"CLARITYCON"); 

このコードのドメインはです。ドメイン名 \ UserName

+0

「https:// CLARITYCON /」または「CLARITYCON」を試しても、私はまだ '401'エラーが表示されます – jdave

+0

は本当にあなたのドメインサーバー名をCLARITYCONですか?このリンクを参照してくださいhttp://stackoverflow.com/questions/36424603/c-sharp-windows-application-sharepoint-login/36425062#36425062あなたは正しいパラメータに問題があると思います – Damirchi

関連する問題