Tag Archives: C#

Twitter Plugin for Windows Live Photo Gallery

Windows Live Essentials comes with lot of awesome applications.

My favorite is Windows Live Photo Gallery. Nice way of organizing and sharing your photos. Most favorite feature is facial recognition, which let you find photos by faces.

After adding photos it starts recognizing faces and ask you to tag the persons in the photos. After tagging people the application starts guessing the persons in the photos and asks if the guess was correct or not. And it gets better at it very fast.

So in the future, whenever you add new photos it will automatically tag people. I started adding really old photos, and I was surprised by its ability to guess that it’s me in a very old photo when I was about 3 years old! So smart!

Other great feature is how easy it is to share photos and videos. Out of the box you can submit your files to SkyDrive (Awesome free 25GB space from Microsoft), Flickr, Youtube and others. But I couldn’t find an option for Twitter!

That’s why I started making a Twitter plugin for Windows Live Photo Gallery. And I made it open source since I couldn’t find any open source plugin for the Windows Live Photo Gallery.

I used CodePlex to host the project and TortoiseHg for distributed revision control system.

Please note that this project is not complete yet. What is left for now is the integration with Windows Live Gallery.

What I’ve finished and is working now is uploading a photo to TwitPic and then post it to Twitter along with a message. What’s left is only using the Windows Live Gallery SDK to communicate with this little application.

The project is hosted here: http://twitter4wlpg.codeplex.com/

You are welcome if you would like to give a hand 🙂

Share

How to Dispose a Form in C# without closing the Application

As a good programmer you should be thinking about the resources your software uses and be as gentle as possible on the Computer’s memory.

With C# you would be probably clearing the objects you no longer need from the memory using the Dispose method.

But if you dispose a Windows Form in C#, the whole application will quit! WHY?

That’s because by default C# select a start up form defined in the Program.cs file like this:

Application.Run(new Form1());

So to fix this just delete the “new Form1()” , and load up an instance of the form you want from Main() in the Program.cs so the whole thing will look like:

Form1 frm1 = new Form1();
frm1.Show();
Application.Run();

Now you can load and dispose forms all you want without the application quiting on you:)

Share

Read Sharepoint 2007 Scale Survey with some statistics

Just an hour before letting the employees hit the survey link on our Sharepoint server, the department changed their mind and they doesn’t want the Single Choice for each question Survey I did. They wanted to change the type of the survey from Multiple radio buttons choices for each question(Which returns one Choice) to Scale Survey (GridChoice Survey in Sharepoint terms or Rating Survey) where the vote is of 1 to 5 scale for each point in a question.

Sharepoint does have a Scale Survey, but if you glimps at it you can’t tell who got the best score, which is something they wanted for the “Employee of the month”.

So in the same way I did to read the items of the Choice Survey, I did it again with some modifications. So instead of looking for the “Choice” type, I used “GridChoice”. Also, the value of the response is different. In a GridChoice value you woud see something like:

Co-operation#4#Skills#3#Outgoing#4#Ideas#3#Attendance#2#

This means the employee got a vote of 4 out of 5 for Co-operation,3 for Skills,4 for Outgoing,3 for Ideas and 2 for Attendance.

So now all I had to do is find a way to extract the numbers out of the string and sum up the numbers for each employe. To do that I did the following C# function:


  private int ResponceScore(string expr)
  {
  String[] numbers = System.Text.RegularExpressions.Regex.Split(expr, @"[^\d+]");
  int total = 0;
  foreach (String number in numbers)
  {
  if (number != "")
  {
  int num = int.Parse(number);
  total += num;
  }
  }
  return total;
  }

What the funtion does is it recieves the response’s text, use a regular expression to select or extract the numbers in the string, sum up the numbers of the response and send it back to be saved in an array or in a variable (Like adding to a variable for each employee).

So the final code is something like:

 

foreach (SPListItem item in SurveyList.Items)
  {
  foreach (SPField field in item.Fields)
  {
  if (field.TypeAsString == “GridChoice”)
  {
  if (field.Title.ToString() == “Employee Name”)
  {
  empScore+= ResponceScore(item[field.Title].ToString());
  }
}
}

So that is how I managed to do a web part to show the top scores in a Scale Survey in Sharepoint.

Have fun 😉
kick it on DotNetKicks.com

Share

How to read Sharepoint 2007 Survey list and get some statistics

Sharepoint Services is awesome new framework from Microsoft. And these days where I work, I mostly do Sharepoint 2007 and MOSS development.

I got this request where they wanted a Survey. Sharepoint does have survey, but it lacks statistics on the survey. In their Survey, the Choices for all the questions are identical. For example voting on “Employee of the Month”. MOSSAfter responding to this survey, they wanted to show the top employee in each Choice. The style of the answers are Choice type, where the user choose one of the option under the name of the employee. For example:
– Emplyee name 1
Choice 1: Co-Operation
Choice 2: Great Example

– Emplyee name 2
Choice 1: Co-Operation
Choice 2: Great Example
.
.
.

So the best solution is to make your Web Part read from that Survey’s items.

And to do that, we just need to loop through the Items of the Survey List, look for the “Choice” type of field in it and read its content (The choosen Choice. Choice 1 or Choice 2 … etc). The title of the field is the question text(Employee name).

So we need two loops one to look for the items, and one to look for items’ fields with the type “Choice” – then its title and content.

This is the code to do the loops and readings:

using (SPSite mySite = SPContext.Current.Web.Site)
{
using (SPWeb myWeb = mySite.OpenWeb())
{
SPList SurveyList = myWeb.Lists["Employee of the month"];
foreach (SPListItem item in SurveyList.Items)
{
foreach (SPField field in item.Fields)
{
if (field.TypeAsString == "Choice")
{
if (item[field.Title].ToString() == "Co-operation")
{
coop += field.Title.ToString() + ",";
}
else if (item[field.Title].ToString() == "Great Example")
{
example += field.Title.ToString() + ",";
}
}
}
}
}
}

Here I used normal string variables to store the names of the employees for each Choice, maybe you would like to use some sort of array instead, but I didn’t since I dont have many questions.

So when the code run, I will have something like the following in my variables:
Co-Operation variable: “Hasan B, Ahmad D, Farah H, Aya K, Hasan B, Amal K, Ahmad D”
Great Example variable: “Tahseen K, Sebhi J, Jafar A, Tahseen K”

Now by counting how many times each name is repeated for each choice, I can get an idea of who got the most votes for each choice.

I used the following C# function to count how many times each phrase or word accured in a string variable:

private string CountWords(string input)
{
char[] delims = new char[] { '.', ',', ';' };
string[] wordlist = input.Split(delims, StringSplitOptions.RemoveEmptyEntries);
SortedList words = new SortedList();
foreach (string item in wordlist)
{
if (!words.ContainsKey(item))
words.Add(item, 1);
else
words[item]++;
}
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair kvp in words)
sb.Append(kvp.Key + ": " + kvp.Value + "
");
return sb.ToString();
}

So now from my Web Part I can write:

writer.Write("Co-Operation" + "
" + CountWords(coop) + "
" +
"Great Example" + "
" + CountWords(example))

And the function CountWords will return me the number of times a name accured in that passed string variable which represents the Choice.

For my example the output would look like:

Co-Operation
Hasan B: 2
Ahmad D: 2
Aya K: 1
Amal K: 1
Farah H:1
Great Example
Tahseen K: 2
Jafar A: 1
Sebhi J: 1

This code needs some additions to make it more useful, but I searched on the net, and I didn’t find answer for this, so I thought I would share with you what I got so far. I’m probably going to work more on this.

Hope it helped.
kick it on DotNetKicks.com

Share