SharePoint高级内容--访问群体对象模型的开发之二
4、为访问群体添加复杂的基于AND 、OR和()的规则
我们可以使用括号与AND ,OR共同作用,组合出更复杂的规则来。 Sharepoint Portal Server的对象模型可以支持最多三层括号的嵌套。
注意:如果一个访问群体对应复杂的规则,您就不能在Web管理页面中查看或编辑其属性了。不过不用担心,您仍可以在Web管理页面中查看其包含的成员。
TopologyManager topology = new TopologyManager();
PortalSite portal = topology.PortalSites[new Uri("http://server_name")];
PortalContext context = PortalApplication.GetContext(portal);
AudienceManager AudMgr = new AudienceManager(context);
Audience a = null;
bool ruleListNotEmpty = false;
try
{
a = AudMgr.Audiences["Engineer"];
}
catch(AudienceArgumentException ex)
{}
ArrayList aRules = a.AudienceRules;
if( aRules == null )
{
aRules = new ArrayList();
}
else
{
ruleListNotEmpty = true;
}
try
{
if (ruleListNotEmpty)
{
aRules.Add(new AudienceRuleComponent(null, "AND", null));
}
AudienceRuleComponent r0 = new AudienceRuleComponent(null, "(", null);
aRules.Add(r0);
AudienceRuleComponent r1 = new AudienceRuleComponent("FirstName", "Contains", "a");
aRules.Add(r1);
AudienceRuleComponent r2 = new AudienceRuleComponent(null, "AND", null);
aRules.Add(r2);
AudienceRuleComponent r3 = new AudienceRuleComponent("WorkEmail", "Contains", "DepA.com");
aRules.Add(r3);
AudienceRuleComponent r4 = new AudienceRuleComponent(null, ")", null);
aRules.Add(r4);
AudienceRuleComponent r5 = new AudienceRuleComponent(null, "OR", null);
aRules.Add(r5);
AudienceRuleComponent r6 = new AudienceRuleComponent(null, "(", null);
aRules.Add(r6);
AudienceRuleComponent r7 = new AudienceRuleComponent("FirstName", "Contains", "b");
aRules.Add(r7);
AudienceRuleComponent r8 = new AudienceRuleComponent(null, "AND", null);
aRules.Add(r8);
AudienceRuleComponent r9 = new AudienceRuleComponent("WorkEmail", "Contains", "DepB.com");
aRules.Add(r9);
AudienceRuleComponent r10 = new AudienceRuleComponent(null, ")", null);
aRules.Add(r10);
a.AudienceRules = aRules;
a.Commit();
}
catch(AudienceException e)
{}
5、获取访问群体的成员PortalSite portal = topology.PortalSites[new Uri("http://server_name")];
PortalContext context = PortalApplication.GetContext(portal);
AudienceManager AudMgr = new AudienceManager(context);
Audience a = null;
bool ruleListNotEmpty = false;
try
{
a = AudMgr.Audiences["Engineer"];
}
catch(AudienceArgumentException ex)
{}
ArrayList aRules = a.AudienceRules;
if( aRules == null )
{
aRules = new ArrayList();
}
else
{
ruleListNotEmpty = true;
}
try
{
if (ruleListNotEmpty)
{
aRules.Add(new AudienceRuleComponent(null, "AND", null));
}
AudienceRuleComponent r0 = new AudienceRuleComponent(null, "(", null);
aRules.Add(r0);
AudienceRuleComponent r1 = new AudienceRuleComponent("FirstName", "Contains", "a");
aRules.Add(r1);
AudienceRuleComponent r2 = new AudienceRuleComponent(null, "AND", null);
aRules.Add(r2);
AudienceRuleComponent r3 = new AudienceRuleComponent("WorkEmail", "Contains", "DepA.com");
aRules.Add(r3);
AudienceRuleComponent r4 = new AudienceRuleComponent(null, ")", null);
aRules.Add(r4);
AudienceRuleComponent r5 = new AudienceRuleComponent(null, "OR", null);
aRules.Add(r5);
AudienceRuleComponent r6 = new AudienceRuleComponent(null, "(", null);
aRules.Add(r6);
AudienceRuleComponent r7 = new AudienceRuleComponent("FirstName", "Contains", "b");
aRules.Add(r7);
AudienceRuleComponent r8 = new AudienceRuleComponent(null, "AND", null);
aRules.Add(r8);
AudienceRuleComponent r9 = new AudienceRuleComponent("WorkEmail", "Contains", "DepB.com");
aRules.Add(r9);
AudienceRuleComponent r10 = new AudienceRuleComponent(null, ")", null);
aRules.Add(r10);
a.AudienceRules = aRules;
a.Commit();
}
catch(AudienceException e)
{}
下面的代码将访问群体的成员的WindowsNT系统名称显示了出来。
TopologyManager topology = new TopologyManager();
PortalSite portal = topology.PortalSites[new Uri("http://server_name")];
PortalContext context = PortalApplication.GetContext(portal);
AudienceManager AudMgr = new AudienceManager(context);
try
{
ArrayList memarray = AudMgr.Audiences["Engineer"].GetMembership();
foreach(UserInfo o in memarray)
{
Console.WriteLine(o.NTName);
}
}
catch(AudienceException e)
{}
6、显示用户所隶属于的访问群体PortalSite portal = topology.PortalSites[new Uri("http://server_name")];
PortalContext context = PortalApplication.GetContext(portal);
AudienceManager AudMgr = new AudienceManager(context);
try
{
ArrayList memarray = AudMgr.Audiences["Engineer"].GetMembership();
foreach(UserInfo o in memarray)
{
Console.WriteLine(o.NTName);
}
}
catch(AudienceException e)
{}
已此类推,我们也可以显示一个用户所隶属于德所有访问群体。
TopologyManager topology = new TopologyManager();
PortalSite portal = topology.PortalSites[new Uri("http://server_name")];
PortalContext context = PortalApplication.GetContext(portal);
AudienceManager AudMgr = new AudienceManager(context);
try
{
//传入一个Windows帐号名来获取隶属于的访问群体组。
//如果要获取当前用户的隶属访问群体组,只要不指定任何参数直接GetUserAudienceIDs()就可以了
ArrayList audienceIDNames = AudMgr.GetUserAudienceIDs("domain_name\\alias");
ArrayList audienceNames = new ArrayList();
for (int i=0; i<audienceIDNames.Count; i++)
{
AudienceNameID arrTemp = (AudienceNameID) audienceIDNames[i];
audienceNames.Add(arrTemp.AudienceName);
Console.WriteLine(audienceNames[i].ToString());
}
}
catch(AudienceException e)
{}
PortalSite portal = topology.PortalSites[new Uri("http://server_name")];
PortalContext context = PortalApplication.GetContext(portal);
AudienceManager AudMgr = new AudienceManager(context);
try
{
//传入一个Windows帐号名来获取隶属于的访问群体组。
//如果要获取当前用户的隶属访问群体组,只要不指定任何参数直接GetUserAudienceIDs()就可以了
ArrayList audienceIDNames = AudMgr.GetUserAudienceIDs("domain_name\\alias");
ArrayList audienceNames = new ArrayList();
for (int i=0; i<audienceIDNames.Count; i++)
{
AudienceNameID arrTemp = (AudienceNameID) audienceIDNames[i];
audienceNames.Add(arrTemp.AudienceName);
Console.WriteLine(audienceNames[i].ToString());
}
}
catch(AudienceException e)
{}
7、得到规程操作符的显示名称和内部名称
可以用下面的代码看看这些操作符在Web管理页面中怎么叫。
TopologyManager topology = new TopologyManager();
PortalSite portal = topology.PortalSites[new Uri("http://server_name")];
PortalContext context = PortalApplication.GetContext(portal);
AudienceManager AudMgr = new AudienceManager(context);
ArrayList OpList = AudMgr.AudienceOperatorList;
for (int list=0; list < OpList.Count; list++)
{
Console.WriteLine(list.ToString());
Console.WriteLine(" Name: {0}" , ((AudienceOperator)OpList[list]).OperatorName);
Console.WriteLine(" DisplayName: {0}" , ((AudienceOperator)OpList[list]).OperatorDisplayName);
}
PortalSite portal = topology.PortalSites[new Uri("http://server_name")];
PortalContext context = PortalApplication.GetContext(portal);
AudienceManager AudMgr = new AudienceManager(context);
ArrayList OpList = AudMgr.AudienceOperatorList;
for (int list=0; list < OpList.Count; list++)
{
Console.WriteLine(list.ToString());
Console.WriteLine(" Name: {0}" , ((AudienceOperator)OpList[list]).OperatorName);
Console.WriteLine(" DisplayName: {0}" , ((AudienceOperator)OpList[list]).OperatorDisplayName);
}
8、得到规则操作符左侧操作数允许使用的名称
可以用下面的代码看看可以用作规则操作符左侧操作数的所有内容。包括在Web管理页面中显示的名称和我们在编程时使用的内部名称。上面有提到的“Everyone”,“DL”就包括在这里,还有好多活动目录中有的属性,值得细细去查看。
TopologyManager topology = new TopologyManager();
PortalSite portal = topology.PortalSites[new Uri("http://server_name")];
PortalContext context = PortalApplication.GetContext(portal);
AudienceManager AudMgr = new AudienceManager(context);
ArrayList LeftContentList;
LeftContentList = AudMgr.AudienceLeftContentList;
for (int list=0; list < LeftContentList.Count; list++)
{
Console.WriteLine(list.ToString());
Console.WriteLine(" Name: " + ((AudienceLeftContent)LeftContentList[list]).Name);
Console.WriteLine(" DisplayName: " + ((AudienceLeftContent)LeftContentList[list]).DisplayName);
Console.WriteLine(" DataType: " + ((AudienceLeftContent)LeftContentList[list]).DataType);
Console.WriteLine(" DataLength: " + ((AudienceLeftContent)LeftContentList[list]).DataLength);
Console.WriteLine(" bProperty: " + ((AudienceLeftContent)LeftContentList[list]).bProperty);
}
(完)PortalSite portal = topology.PortalSites[new Uri("http://server_name")];
PortalContext context = PortalApplication.GetContext(portal);
AudienceManager AudMgr = new AudienceManager(context);
ArrayList LeftContentList;
LeftContentList = AudMgr.AudienceLeftContentList;
for (int list=0; list < LeftContentList.Count; list++)
{
Console.WriteLine(list.ToString());
Console.WriteLine(" Name: " + ((AudienceLeftContent)LeftContentList[list]).Name);
Console.WriteLine(" DisplayName: " + ((AudienceLeftContent)LeftContentList[list]).DisplayName);
Console.WriteLine(" DataType: " + ((AudienceLeftContent)LeftContentList[list]).DataType);
Console.WriteLine(" DataLength: " + ((AudienceLeftContent)LeftContentList[list]).DataLength);
Console.WriteLine(" bProperty: " + ((AudienceLeftContent)LeftContentList[list]).bProperty);
}