Some SharePoint Portal OM basic samples


Refer to http://blogs.msdn.com/dwinter/archive/2005/03/01/383306.aspx (Portal OM) and http://blogs.msdn.com/dwinter/archive/2005/02/15/373076.aspx (WSS OM) for setup if you are not familiar with creating a SharePoint OM application.

We'll start with a C# Windows Form application.  I am using Microsoft.SharePoint, Microsoft.SharePoint.Portal, Microsoft.SharePoint.Portal.Alerts, Microsft.SharePoint.Portal.UserProfiles, Microsoft.SharePoint.Portal.Topology in these samples.  Use the same kind of setup in your designer that I showed in the initial Portal OM post.

To keep up with how I showed SPSite.Delete and mass alert management when introducing WSS OM, here are some simple examples of similiar tasks on the Portal side of the house.
Here is a delete portal:

private void button1_Click(object sender, System.EventArgs e)
{
      TopologyManager myTopologyManager =
new TopologyManager();
      PortalSite myPortalSite = myTopologyManager.PortalSites[
new Uri(textBox1.Text)];
      try
      {
            //You can also specify a bool of true here if you wish to remove the databases
            myTopologyManager.PortalSites.Delete(myPortalSite); 
      }
      catch (Exception ex)
      {
            listBox2.Items.Add(ex.Message);
      }
}

Here is some alerts based code just to get you in the right direction.  If you knew a specific alert ID you could operate against it--or you could enumerate all, look for something specific and then act accordingly.  Here is an example on doing a mass removal.  I have never had to use this... I doubt I would want to because I would want to enumerate and have some more control.

      //using Microsoft.SharePoint.Portal.Alerts
      Administration myAlertsAdministration =
new Administration();
      myAlertsAdministration.DeleteAllSubscriptions();

Here is a quick sample on enumerating alerts within Portal and doing a variety of non-specific tasks (which of course you wouldn't do all at once).  Here I get to show one of the many entrypoints that utilizes PortalContext.

      //using Microsoft.SharePoint.Portal.UserProfiles
      UserProfileManager myUserProfileManager =
new UserProfileManager(myPortalContext);
      UserProfile tempUserProfile = myUserProfileManager.GetUserProfile(textBox1.Text);
      foreach (UserProfile myUserProfile in myUserProfileManager)
      {
            myUserProfile.Alerts.ActivateAll();
            myUserProfile.Alerts.DeactivateAll();
            myUserProfile.Alerts.DeleteAllResults();
            myUserProfile.Alerts.DeleteAll();
      }

As you can imagine, there are a number of possibilities here in what and how you can accomplish your specific task.  You could even add alerts programatically with a little finesse.

Skip to main content