2

リモート サーバーでホストされている SharePoint 2010 サイトがあり、C# を使用して別のマシンから SharePoint グループの 1 つにユーザーを自動的に追加したいと考えています。以下のように、インターネット全体で言及されているWebサービスを使用してみました。

class Program
{
    static void Main(string[] args)
    {
        SPUserGroupRef.UserGroup usrGrpService = new SPUserGroupRef.UserGroup();
        System.Net.NetworkCredential netCred = new System.Net.NetworkCredential(<my username>, <my password>, <my domain name>);
        usrGrpService.Credentials = netCred;
        usrGrpService.AddUserToGroup(<group name>, <new user full name>, <domain\new user name>, <new user email address>, <notes>);
    }
}

注: 1. SPUserGroupRef は、http:///_vti_bin/usergroup.asmx への Web サービス参照です。 2. 上記のコード内の山かっこで囲まれたエンティティはすべて文字列です。

Main 内のコードの最初の 3 行は正常に実行されますが、4 行目は「Microsoft.SharePoint.SoapServer.SoapServerException の例外がスローされました」というエラーで失敗します。私は SharePoint を初めて使用するので、ここで何か不足している可能性があります。誰かがこの問題を解決する方法を理解するのを手伝ってくれますか、またはうまくいくかもしれない別のアプローチを提案してくれるかもしれません (ただし、私の SharePoint サイトはローカル マシンではなく、リモート サーバーでホストされていることを思い出してください。

4

1 に答える 1

1

Sharepoint 2010 クライアント オブジェクト モデルを使用しないのはなぜですか。以下のコードはテストしていませんが、動作するはずです。

namespace ClientObjectModel 
{ 
class Program 
{ 
static void Main(string[] args) 
{ 
// Add a new user to a particular group 

string siteURL = "http://serverName:1111/sites/SPSiteDataQuery/"; 
ClientContext context = new ClientContext(siteURL); 
GroupCollection groupColl = context.Web.SiteGroups; 
Group group = groupColl.GetById(7); 
UserCreationInformation userCreationInfo = new UserCreationInformation(); 
userCreationInfo.Email = ""; 
userCreationInfo.LoginName = @"DomainName\UserName"; 
userCreationInfo.Title = "SharePoint Developer"; 
User user = group.Users.Add(userCreationInfo); 
context.ExecuteQuery(); 
} 
} 
}

Microsoft.SharePoint と Microsoft.SharePoint.Client は動作するかどうかを教えてくれますか?

于 2012-12-25T03:51:20.600 に答える