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();
                }
            }
        }
    }
}

Wednesday, March 27, 2013

Delete a Field(Column) in SharePoint List using PowerShell

Hello People,
The following is a code snippet from PowerShell to completely remove or delete a field in a SharePoint list. The code uses the GUID to find the field (column). I tested this with SharePoint 2010. Feel free to modify the code. :)

#Source Help - http://dotnetfollower.com/wordpress/2012/07/sharepoint-how-to-delete-a-list-fieldcolumn-programmatically/

$web=Get-SPWeb "http://mySite/"
$list=$web.Lists.TryGetList("My List Name")
if ($list -ne $null) {
    foreach($column in $list.Fields){
        if ($column.Id -eq "[GUID]") {
            write-host -f green "Deleting column with Internal name as : " $column.InternalName
            
            if ($column.ReadOnlyField)
            {
                $column.ReadOnlyField = $false;
                $column.Update()
            }

            if ($column.Hidden)
            {
                $column.Hidden = $false;
                $column.Update()
            }
     
            if ($column.AllowDeletion -eq $null -or !$column.AllowDeletion.Value)
            {
                $column.AllowDeletion = $true
                $column.Update()
            }
     
            $column.Delete()
            $column.ParentList.Update()

        }
    }
}
else
{
    write-host "list is null"
}

Sunday, March 17, 2013

Force Printer Cleaner

Hello People,

I believe all of you have been stuck on printers where you send files to the printer, But they do not actually print. And later pm you try to delete the file to be printed in the printer tray and they just keep on saying deleting.

Well here is an executable file that will remove the files in the printer tray. No more printer troubles. :D

Force Printer Cleaner

Thanks,
Hemika Kodikara

Tuesday, March 12, 2013

Dummy File Creator

Hello guys,

Herewith I have created a windows application to create empty files with a specific file size. This tool can be used for testing purposes of file upload control of web applications and etc.

Dummy File Creator

Thanks :)

Monday, January 28, 2013

Windows form application to run a PowerShell script file.

Hello Guys,
I have developed a small windows form application to run a PowerShell script file. I havent done many of windows form applications.

The application will only run a script. It will not call any function on the script unless the script it self is calling the functions.
The command "write-host" will give an error if it mentioned on the script. Therefore change the "write-host" commands to "write-output" in your script. "write-output" will actually send an output to the text window.

Feel free to edit the software.


A sample ps1 file is also included.

Download File