Thursday, July 28, 2011

The Mystery of SPWeb.SiteGroups and SPWeb.Groups

I've spend two days to find out how to create groups which is partaining just to Subsite. Everytime I created a group using SPWeb.SiteGroups.Add the group is associated with the parent site rather than subsite.

From documentation I realize
SPWeb.SiteGroups is associated with SiteCollection
SPWeb.Groups is associated with Web

So I attempted SPWeb.Groups.Add. And I encounter the following error message.

“You cannot add a group directly to the Groups collection. You can add a group to the SiteGroups collection.”

Damn!

To create a group for subsite, you have to create site groups first. After creating site group you have to add association of the group with sub site.

Someone from MSDN forum kind enough to give me this solution. I am just sharing it again to save the headache of others.

Pay attention to web.AssociatedGroups.Add. Without this function the group will not be associated to the subsite. :)

//Add the group to the SPWeb web
//This will add the SPGroup to SiteCollection
web.SiteGroups.Add(groupName, owner, defaultuser, description);

//Associate de group to the SPWeb.
//This will add the SPGroup to left side quicklaunch of Manage Groups page
web.AssociatedGroups.Add(web.SiteGroups[groupName]);
web.Update();

//Assignment of the roles.
//This is the actual permission/role assignment of SPGroup
SPRoleAssignment assignment = new SPRoleAssignment(web.SiteGroups[groupName]);
SPRoleDefinition roleApp = web.RoleDefinitions["Contribute"];
assignment.RoleDefinitionBindings.Add(roleApp);
web.RoleAssignments.Add(assignment);
web.Update();

http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/b6a7edda-e5fd-4a58-9273-e12705bec727

Thursday, July 7, 2011

Scanning through tags and notes, comments and User profile data in Sharepoint 2010

I've come across a task where by I need to write a timer job to scan through User profile data and all the social networking tags and notes to policing whether any user has put in any vulgarities or inappropriate comments. After going through the API and some of the testing on my own this is a small prototype of my findings. The program can tell which user has made what kind of changes for all these data. If anyone out there looking for something similar to what I needed to be done I guess this would help someone's live a little easier. :)

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;
using Microsoft.Office.Server.SocialData;

namespace UserProfileChanges
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://yourserver"))
{
SPServiceContext context = SPServiceContext.GetContext(site);
UserProfileManager profileManager =
new UserProfileManager(context);

// this gets some subset of changes to a user profile

DateTime startDate =
DateTime.UtcNow.Subtract(TimeSpan.FromDays(5));
UserProfileChangeQuery changeQuery =
new UserProfileChangeQuery(false, true);
UserProfileChangeToken changeToken =
new UserProfileChangeToken(startDate);
changeQuery.ChangeTokenStart = changeToken;
changeQuery.Anniversary = false;
changeQuery.SingleValueProperty = true;
changeQuery.MultiValueProperty = true;
changeQuery.DistributionListMembership = false;
changeQuery.SiteMembership = false;
changeQuery.UpdateMetadata = true;

UserProfileChangeCollection changes =
profileManager.GetChanges(changeQuery);
foreach (UserProfileChange change in changes)
{
Console.WriteLine("Changes occurs at "+change.EventTime.ToString());
if (change is UserProfilePropertyValueChange)
{
UserProfilePropertyValueChange propertyChange =
(UserProfilePropertyValueChange)change;
if (propertyChange.ChangeType.ToString() == "Metadata")
{
Console.WriteLine("Change Type " + propertyChange.ChangeType.ToString());
Console.WriteLine("Name " + propertyChange.ProfileProperty.Name);
Console.WriteLine("Account Name" + propertyChange.AccountName);
Microsoft.Office.Server.UserProfiles.UserProfile u = profileManager.GetUserProfile(propertyChange.AccountName);
Console.WriteLine("Property Value " + u[propertyChange.ProfileProperty.Name].ToString());
Console.WriteLine();
Console.WriteLine();
}
}
}

UserProfileManager myUserProfileManager = new UserProfileManager(context);
foreach (UserProfile myUserProfile in myUserProfileManager)
{
SocialTagManager mySocialTagManager = new SocialTagManager(context);
SocialTag[] tags = mySocialTagManager.GetTags(myUserProfile);
Console.WriteLine("Tags for user:");
foreach (SocialTag tag in tags)
{
Console.WriteLine(tag.Term.Name + ": " + tag.Url);
Console.WriteLine(tag.Owner.DisplayName);
}

SocialCommentManager mySocialCommentManager = new SocialCommentManager(context);
SocialComment[] comments = mySocialCommentManager.GetComments(myUserProfile);
Console.WriteLine("Comments for user:");
foreach (SocialComment comment in comments)
{
Console.WriteLine(comment.Url + ": " + comment.Comment);
Console.WriteLine(comment.Owner.DisplayName);
}
}


Console.Read();
}

}
}
}