Tuesday, July 9, 2013

Get Start Dates of a Recurring Outlook Appointment

Hi All,

I was able to create a C# stub in order to get the start dates of a recurring appointment in outlook. So far I have not found any errors. Feel free to modify the code if necessary.

private Collection GetOccurrences(DateTime startDate, RecurrencePattern recurrencePattern)
{
    Collection occurrences = new Collection();

    if (recurrencePattern != null && !recurrencePattern.NoEndDate)
    {
        switch (recurrencePattern.RecurrenceType)
        {
            case OlRecurrenceType.olRecursDaily:
                for (int i = 0; i < recurrencePattern.Occurrences; i++)
                {
                    occurrences.Add(startDate);
                    startDate = startDate.AddDays(1 * recurrencePattern.Interval);
                }

                break;
            case OlRecurrenceType.olRecursWeekly:
                if (recurrencePattern.Interval == 0 && (int)recurrencePattern.DayOfWeekMask == 62)
                {
                    int countW = 0;

                    while (countW < recurrencePattern.Occurrences)
                    {
                        if (startDate.DayOfWeek != DayOfWeek.Sunday && startDate.DayOfWeek != DayOfWeek.Saturday)
                        {
                            occurrences.Add(startDate);
                            countW++;
                        }

                        startDate = startDate.AddDays(1);
                    }
                }
                else if (recurrencePattern.Interval > 0)
                {
                    int countW = 0;

                    while (countW < recurrencePattern.Occurrences)
                    {
                        if (recurrencePattern.Interval == 1)
                        {
                            if (this.IsMaskedDay((int)recurrencePattern.DayOfWeekMask, startDate))
                            {
                                occurrences.Add(startDate);
                                countW++;
                            }

                            startDate = startDate.AddDays(1);
                        }
                        else
                        {
                            if (occurrences.Contains(startDate.AddDays(-7)))
                            {
                                startDate = startDate.AddDays(7 * (recurrencePattern.Interval - 1));
                            }

                            if (this.IsMaskedDay((int)recurrencePattern.DayOfWeekMask, startDate))
                            {
                                occurrences.Add(startDate);
                                countW++;
                            }

                            startDate = startDate.AddDays(1);
                        }
                    }
                }

                break;
            case OlRecurrenceType.olRecursMonthly:
                if (recurrencePattern.Instance == 0)
                {
                    for (int i = 0; i < recurrencePattern.Occurrences; i++)
                    {
                        occurrences.Add(startDate);
                        startDate = startDate.AddMonths(recurrencePattern.Interval);
                    }
                }

                break;
            case OlRecurrenceType.olRecursMonthNth:
                occurrences.Add(startDate);
                DateTime dateToAdd = startDate;
                int countMNth = 1;

                while (countMNth < recurrencePattern.Occurrences)
                {
                    dateToAdd = new DateTime(dateToAdd.Year, dateToAdd.AddMonths(recurrencePattern.Interval).Month, 1, dateToAdd.Hour, dateToAdd.Minute, dateToAdd.Second);
                    for (int i = 0; i < 7; i++)
                    {
                        if (this.IsMaskedDay((int)recurrencePattern.DayOfWeekMask, dateToAdd))
                        {
                            int currentMonth = dateToAdd.Month;
                            if (dateToAdd.AddDays(7 * (recurrencePattern.Instance - 1)).Month != currentMonth)
                            {
                                dateToAdd = dateToAdd.AddDays(7 * (recurrencePattern.Instance - 2));
                            }
                            else
                            {
                                dateToAdd = dateToAdd.AddDays(7 * (recurrencePattern.Instance - 1));
                            }

                            occurrences.Add(dateToAdd);
                            countMNth++;
                            break;
                        }
                        else
                        {
                            dateToAdd = dateToAdd.AddDays(1);
                        }
                    }
                }

                break;
            case OlRecurrenceType.olRecursYearly:
                if (recurrencePattern.Instance == 0)
                {
                    int countY = 0;

                    // TODO: Please use a for loop, since the number of iterations is known.
                    while (countY < recurrencePattern.Occurrences)
                    {
                        occurrences.Add(startDate);
                        countY++;
                        //startDate = startDate.AddYears(1);
                        //// Exception popping. please see
                        startDate = startDate.AddYears(recurrencePattern.Interval / 12);
                    }
                }

                break;
            case OlRecurrenceType.olRecursYearNth:
                occurrences.Add(startDate);
                DateTime dateToAddY = startDate;
                int countYNth = 1;

                while (countYNth < recurrencePattern.Occurrences)
                {
                    dateToAddY = new DateTime(dateToAddY.AddYears(recurrencePattern.Interval / 12).Year, dateToAddY.Month, 1, dateToAddY.Hour, dateToAddY.Minute, dateToAddY.Second);
                    for (int i = 0; i < 7; i++)
                    {
                        if (this.IsMaskedDay((int)recurrencePattern.DayOfWeekMask, dateToAddY))
                        {
                            int currentMonth = dateToAddY.Month;
                            if (dateToAddY.AddDays(7 * (recurrencePattern.Instance - 1)).Month != currentMonth)
                            {
                                dateToAddY = dateToAddY.AddDays(7 * (recurrencePattern.Instance - 2));
                            }
                            else
                            {
                                dateToAddY = dateToAddY.AddDays(7 * (recurrencePattern.Instance - 1));
                            }

                            occurrences.Add(dateToAddY);
                            countYNth++;
                            break;
                        }
                        else
                        {
                            dateToAddY = dateToAddY.AddDays(1);
                        }
                    }
                }

                break;
            default:
                break;
        }
    }

    return occurrences;
}

private bool IsMaskedDay(int maskedInt, DateTime date)
{
    string binaryString = Convert.ToString(maskedInt, 2);
    binaryString = new string('0', 7 - binaryString.Length) + binaryString;

    char[] binaryArray = binaryString.ToCharArray();
    Array.Reverse(binaryArray);

    return binaryArray[(int)date.DayOfWeek] == '1';
}

Delete Social Tags in SharePoint 2010 using C#

Hi all,
I've recently wanted to delete social tags when I am deploying my wsp using powershell. I started writing the code and was able to retrieve the social tags but was not able to delete them. When I attempted to delete them an exception was thrown. More like an Unauthorized exception. Read this for more information on it.

But I was able to find this blog post which helped me out in solving the issue.  I was able to create a console program using C# to delete the social tags of SharePoint 2010. I havent tested it much. Feel free to modify the code.
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security.Principal;
using System.Web;
using Microsoft.Office.Server.SocialData;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace RemoveTags
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite(args[0]))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine("Deleting all tags");
                    foreach (SPUser user in site.OpenWeb().AllUsers)
                    {
                        if (!user.IsDomainGroup && user.LoginName != "NT AUTHORITY\\LOCAL SERVICE" && user.LoginName != "SHAREPOINT\\system")
                        {
                            web.AllowUnsafeUpdates = true;
                            Console.WriteLine("Deleting tags for : " + user.Name);
                            HttpRequest request = new HttpRequest("", args[0], "");
                            HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter(CultureInfo.CurrentCulture)));
                            HttpContext.Current.Items["HttpHandlerSPWeb"] = web;
                            WindowsIdentity wi = WindowsIdentity.GetCurrent();
                            typeof(WindowsIdentity).GetField("m_name", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(wi, user.LoginName);
                            HttpContext.Current.User = new GenericPrincipal(wi, new string[0]);
                            WindowsIdentity wi2 = WindowsIdentity.GetCurrent();
                            SPServiceContext serviceContext = SPServiceContext.GetContext(HttpContext.Current);
                            SocialTagManager socialTagManager = new SocialTagManager(serviceContext);
                            SPServiceContext context = SPServiceContext.GetContext(site);
                            UserProfileManager usrPrfMngr = new UserProfileManager(context);
                            UserProfile usrPrf = usrPrfMngr.GetUserProfile(user.LoginName);
                            SocialTag[] tags = socialTagManager.GetTags(usrPrf);

                            foreach (SocialTag tag in tags)
                            {
                                Term term = tag.Term;
                                socialTagManager.DeleteTag(tag.Url, term);

                            }
                        }
                    }

                    web.AllowUnsafeUpdates = false;

                    Console.WriteLine("All tags are deleted");
                    Console.ResetColor();
                    Console.ReadLine();
                }
            }
        }
    }
}