Showing posts with label Programmatically. Show all posts
Showing posts with label Programmatically. Show all posts

Thursday, 3 May 2012

How to upload a user profile photo programmatically

Updated No. 1: Changes required by SP 2010 RTM.

Updated No. 2: Method added to set the picture URL property for the user profile.

On the MSDN forum I’ve met a question that ask about how can one upload and set user profile pictures programmatically. I did a little research on the area and found another thread with the same question but I think the accepted answer of Chakkaradeep Chandran really does not answer the question, as it simply sets the image in the profile through the PictureUrl profile property, but does nothing related to upload and image resizing.

I admit the later tasks are not trivial although definitely not impossible through some hacking with Reflection. It is required since the methods we need (LoadPictureLibraryInternal, CreateThumbnail, GetSubfolderForPictures) are internal and private members of the ProfileImagePicker and UserProfilePhotos classes (later class is internal itself as well). So if you don’t mind this kind of dirty code, here is the solution for the question.

You should create a console application and add references to System.Drawing, System.Web, Microsoft.SharePoint, Microsoft.SharePoint.Portal, Microsoft.Office.Server and Microsoft.Office.Server.UserProfiles assemblies.

Next, use the code below:


  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using System.Drawing;

  6. using System.Reflection;

  7. using Microsoft.SharePoint;

  8. using Microsoft.SharePoint.Portal.WebControls;

  9. using System.IO;

  10. using Microsoft.Office.Server.UserProfiles;

  11. namespace UploadProfileImage

  12. {

  13.     class Program

  14.     {

  15.         static void Main(string[] args)

  16.         {

  17.             Program prog = new Program();

  18.             prog.UploadProfileImages();

  19.         }

  20.         private void UploadProfileImages()

  21.         {

  22.             // my site host url

  23.             String url = "http://sp2010/profiles/host";

  24.             using (SPSite site = new SPSite(url))

  25.             {

  26.                 using (SPWeb web = site.OpenWeb())

  27.                 {

  28.                     ProfileImagePicker profileImagePicker = new ProfileImagePicker();

  29.                     InitializeProfileImagePicker(profileImagePicker, web);

  30.                     SPFolder subfolderForPictures = GetSubfolderForPictures(profileImagePicker);

  31.                     // repeat this block if you have more images and users

  32.                     // your account name

  33.                     String accountName = @"domain\administrator";

  34.                     // path to image file

  35.                     String imageFilePath = @"C:\Temp\AdminAvatar.jpg";

  36.                     UploadPhoto(accountName, imageFilePath, subfolderForPictures, profileImagePicker);

  37.                     SetPictureUrl(accountName, subfolderForPictures);

  38.                     // repeat block finished

  39.                 }

  40.             }

  41.         }

  42.         private void SetPictureUrl(string accountName, SPFolder subfolderForPictures)

  43.         {

  44.             Console.WriteLine("Setting profile image for user '{0}'", accountName);

  45.             SPSite site = subfolderForPictures.ParentWeb.Site;

  46.             UserProfileManager userProfileManager = new UserProfileManager(SPServiceContext.GetContext(site));

  47.             UserProfile userProfile = userProfileManager.GetUserProfile(accountName);

  48.             string fileNameWithoutExtension = GetFileNameFromAccountName(accountName);

  49.             string pictureUrl = String.Format("{0}/{1}/{2}_MThumb.jpg", site.Url, subfolderForPictures.Url, fileNameWithoutExtension);

  50.             userProfile["PictureUrl"].Value = pictureUrl;

  51.             userProfile.Commit();

  52.         }

  53.         private void UploadPhoto(string accountName, string imageFilePath, SPFolder subfolderForPictures, ProfileImagePicker profileImagePicker)

  54.         {

  55.             Console.WriteLine("Uploading image '{0}' for user '{1}'", imageFilePath, accountName);

  56.             if (!File.Exists(imageFilePath) || Path.GetExtension(imageFilePath).Equals(".gif"))

  57.             {

  58.                 Console.WriteLine("File '{0}' does not exist or has invalid extension", imageFilePath);

  59.             }

  60.             else

  61.             {

  62.                 string fileNameWithoutExtension = GetFileNameFromAccountName(accountName);

  63.                 FileStream file = File.Open(imageFilePath, FileMode.Open);

  64.                 BinaryReader reader = new BinaryReader(file);

  65.                 if (subfolderForPictures != null)

  66.                 {

  67.                     // try casting length (long) to int

  68.                     byte[] buffer = reader.ReadBytes((int)file.Length);

  69.                     int largeThumbnailSize = 0×90;

  70.                     int mediumThumbnailSize = 0×60;

  71.                     int smallThumbnailSize = 0×20;

  72.                     using (MemoryStream stream = new MemoryStream(buffer))

  73.                     {

  74.                         using (Bitmap bitmap = new Bitmap(stream, true))

  75.                         {

  76.                             CreateThumbnail(bitmap, largeThumbnailSize, largeThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_LThumb.jpg");

  77.                             CreateThumbnail(bitmap, mediumThumbnailSize, mediumThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_MThumb.jpg");

  78.                             CreateThumbnail(bitmap, smallThumbnailSize, smallThumbnailSize, subfolderForPictures, fileNameWithoutExtension + "_SThumb.jpg");

  79.                         }

  80.                     }

  81.                 }

  82.             }

  83.         }

  84.         private void InitializeProfileImagePicker(ProfileImagePicker profileImagePicker, SPWeb web)

  85.         {

  86.             Type profileImagePickerType = typeof(ProfileImagePicker);

  87.             FieldInfo fi_m_objWeb = profileImagePickerType.GetField("m_objWeb", BindingFlags.NonPublic | BindingFlags.Instance);

  88.             fi_m_objWeb.SetValue(profileImagePicker, web);

  89.             MethodInfo mi_LoadPictureLibraryInternal = profileImagePickerType.GetMethod("LoadPictureLibraryInternal", BindingFlags.NonPublic | BindingFlags.Instance);

  90.             if (mi_LoadPictureLibraryInternal != null)

  91.             {

  92.                 mi_LoadPictureLibraryInternal.Invoke(profileImagePicker, new object[] { });

  93.             }

  94.         }

  95.         public SPFile CreateThumbnail(Bitmap original, int idealWidth, int idealHeight, SPFolder folder, string fileName)

  96.         {

  97.             SPFile file = null;

  98.             // hack to get the Microsoft.Office.Server.UserProfiles assembly

  99.             Assembly userProfilesAssembly = typeof(UserProfile).Assembly;

  100.             // or assuming you know all the details of the assembly

  101.             // Assembly userProfilesAssembly = Assembly.Load(“Microsoft.Office.Server.UserProfiles, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”);

  102.             // UserProfilePhotos is internal class,

  103.             // so you cannot get it directly from Visual Studio

  104.             Type userProfilePhotosType = userProfilesAssembly.GetType("Microsoft.Office.Server.UserProfiles.UserProfilePhotos");

  105.             MethodInfo mi_CreateThumbnail = userProfilePhotosType.GetMethod("CreateThumbnail", BindingFlags.NonPublic | BindingFlags.Static);

  106.             if (mi_CreateThumbnail != null)

  107.             {

  108.                 file = (SPFile)mi_CreateThumbnail.Invoke(null, new object[] { original, idealWidth, idealHeight, folder, fileName });

  109.             }

  110.             return file;

  111.         }

  112.         private SPFolder GetSubfolderForPictures(ProfileImagePicker profileImagePicker)

  113.         {

  114.             SPFolder folder = null;

  115.             Type profileImagePickerType = typeof(ProfileImagePicker);

  116.             MethodInfo mi_GetSubfolderForPictures = profileImagePickerType.GetMethod("GetSubfolderForPictures", BindingFlags.NonPublic | BindingFlags.Instance);

  117.             if (mi_GetSubfolderForPictures != null)

  118.             {

  119.                 folder = (SPFolder)mi_GetSubfolderForPictures.Invoke(profileImagePicker, new object[] { });

  120.             }

  121.             return folder;

  122.         }

  123.         private string GetFileNameFromAccountName(string accountName)

  124.         {

  125.             string result = accountName;

  126.             string charsToReplace = @"\/:*?""<>|";

  127.             Array.ForEach(charsToReplace.ToCharArray(), charToReplace => result = result.Replace(charToReplace, '_'));

  128.             return result;

  129.         }

  130.     }

  131. }


 

 

Happy profile image uploading!

You should know that user profile photos are stored in three different resolutions in a folder called Profile Pictures in the User Photos image library on the My Site Host site.

Note, that I found a bit strange dependency of behavior on the size of the photo you set in the PictureUrl profile property when it is about removing the profile image. It seems to be a minor issue, but might have significant impact on your solution.

When you upload the profile photo via the Edit User Profile user interface and then removes that also via the UI, then all of the image versions (I mean the three different resolution) are deleted. and using code. I consider this to be the standard behavior as it is not desired to leave orphaned files in the User Photos image library.

First I’ve tried to set the large image (_LThumb.jpg) from code in the PictureUrl profile property, but when I was to remove the image from the UI then only the large image was deleted, medium and small images remained in the image library. After checking the effect of setting the image from the UI I found, that it sets the PictureUrl property  to the medium image (_MThumb.jpg), so I modified the code accordingly. Now all of the images were removed as expected.

Friday, 17 February 2012

Convert Office Documents (.docx, .pptx, .pub) into PDF Programmatically

Word

========

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Office.Interop.Word;

 

namespace DocConverter

{

class Program

{

static void Main(string[] args)

{

ApplicationClass wordApplication = new ApplicationClass();

 

Document wordDocument = null;

object paramSourceDocPath = @"D:\Fun\Test.docx";

object paramMissing = Type.Missing;

string paramExportFilePath = @"D:\Fun\Test.pdf";

WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;

bool paramOpenAfterExport = false;

WdExportOptimizeFor paramExportOptimizeFor =

WdExportOptimizeFor.wdExportOptimizeForPrint;

WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;

int paramStartPage = 0;

int paramEndPage = 0;

WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;

bool paramIncludeDocProps = true;

bool paramKeepIRM = true;

WdExportCreateBookmarks paramCreateBookmarks =

WdExportCreateBookmarks.wdExportCreateWordBookmarks;

bool paramDocStructureTags = true;

bool paramBitmapMissingFonts = true;

bool paramUseISO19005_1 = false;

try

{

// Open the source document.

wordDocument = wordApplication.Documents.Open(

ref paramSourceDocPath, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing);

 

// Export it in the specified format.

if (wordDocument != null)

wordDocument.ExportAsFixedFormat(paramExportFilePath,

paramExportFormat, paramOpenAfterExport,

paramExportOptimizeFor, paramExportRange, paramStartPage,

paramEndPage, paramExportItem, paramIncludeDocProps,

paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,

paramBitmapMissingFonts, paramUseISO19005_1,

ref paramMissing);

}

catch (Exception ex)

{

// Respond to the error

}

finally

{

// Close and release the Document object.

if (wordDocument != null)

{

wordDocument.Close(ref paramMissing, ref paramMissing,

ref paramMissing);

wordDocument = null;

}

 

// Quit Word and release the ApplicationClass object.

if (wordApplication != null)

{

wordApplication.Quit(ref paramMissing, ref paramMissing,

ref paramMissing);

wordApplication = null;

}

 

GC.Collect();

GC.WaitForPendingFinalizers();

GC.Collect();

GC.WaitForPendingFinalizers();

}

 

}

}

}

 

PowerPoint

=============

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Office.Interop.PowerPoint;

namespace PowerPointConverter

{

class Program

{

static void Main(string[] args)

{

Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();

Microsoft.Office.Interop.PowerPoint.Presentation presentation = ppApp.Presentations.Open(@"D:\Fun\Test.pptx", Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);

presentation.ExportAsFixedFormat(@"D:\Fun\Test.xps",

PpFixedFormatType.ppFixedFormatTypeXPS,

PpFixedFormatIntent.ppFixedFormatIntentPrint,

Microsoft.Office.Core.MsoTriState.msoFalse,

PpPrintHandoutOrder.ppPrintHandoutHorizontalFirst,

PpPrintOutputType.ppPrintOutputSlides,

Microsoft.Office.Core.MsoTriState.msoFalse,

null,

PpPrintRangeType.ppPrintAll,

"",

false,

false,

false,

true,

true,

System.Reflection.Missing.Value);

presentation.Close();

presentation = null;

ppApp = null;

GC.Collect();

 

}

}

}

 

Publisher
===============

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Office.Interop.Publisher;

using System.Reflection;

 

namespace PublisherConverter

{

class Program

{

static void Main(string[] args)

{

Microsoft.Office.Interop.Publisher.Application pbApp = new Microsoft.Office.Interop.Publisher.Application();

Microsoft.Office.Interop.Publisher.Document pbDoc = pbApp.Open(@"D:\Fun\Test4.pub", true, true, PbSaveOptions.pbDoNotSaveChanges);

pbDoc.ExportAsFixedFormat(PbFixedFormatType.pbFixedFormatTypePDF, @"D:\Fun\Test.pdf", PbFixedFormatIntent.pbIntentPrinting, true, 300, 450, 1200, 1800, 1, pbDoc.Pages.Count, 1, false, PbPrintStyle.pbPrintStyleDefault, false, false, false, System.Reflection.Missing.Value);

pbDoc.Close();

pbDoc = null;

pbApp = null;

GC.Collect();

}

}

}

Friday, 16 December 2011

Converting login name to claim and vice versa in programmatically

SharePoint 2010 introduced Claims Based Authentication. One of the consequences of this is the fact that in order to use Forms Based Authentication (FBA) you need to configure your Web Application to use Claims instead of Classic Authentication. One of the many changes that you notice while working with claims are different login names: while in SharePoint 2007 you used something like myprovider:myuser, SharePoint 2010 makes the claims-soup of it: i:0#.f|myprovider|myuser. And while this is something you can take into account for newly created solutions, it can get confusing when upgrading SharePoint 2007 solutions to SharePoint 2010, especially if all you need is the user name. So is String.Replace the only way to get it out or is there a better way?

It turns out that retrieving the user name from its claims representation is pretty straight forward and can be done using the following code snippet:










string userName = null;

SPClaimProviderManager mgr = SPClaimProviderManager.Local;

if (mgr != null)

{

    userName = mgr.DecodeClaim(SPContext.Current.Web.CurrentUser.LoginName).Value;

}




First we retrieve a reference to the Claims Provider Manager configured with the current Web Application. Then, using the DecodeClaim(string) method, we convert the string into SPClaim and retrieve its value, which contains the login name of the current user.

So, assuming you were logged in with the myuser account and the value of the SPContext.Current.Web.CurrentUser.LoginName property was something similar to i:0#.f|myprovider|myuser, calling the code snippet above would return myuser.

Claims back and forth


In some scenario’s you might want to do the exact opposite: you might be starting off with a login name and will need to turn it over into the claims-based name. Just like in the previous scenario this can be easily done using the SPClaimProviderManager:










string userName = null;

SPClaimProviderManager mgr = SPClaimProviderManager.Local;

if (mgr != null)

{

    SPClaim claim = new SPClaim(SPClaimTypes.UserLogonName, "myuser", "http://www.w3.org/2001/XMLSchema#string", SPOriginalIssuers.Format(SPOriginalIssuerType.Forms, "myprovider"));

    userName = mgr.EncodeClaim(claim);

}




Just as in the previous example we start off by getting a reference to the current Claims Provider Manager. The next step is to create a claim based on the login name of the current user. You can do this using the constructor of the SPClaim class. As the type you have to provide SPClaimTypes.UserLogonName, as the value – the login name of the user, as the value type, the XML type for string and finally, as the originalIssuer the name of your Membership Provider. In this sample I used a custom Forms Based Membership Provider but you might need some other type depending on your scenario.

In our example, if you needed the claims representation for the the myuser claims account, calling the above code snippet would return i:0#.f|myprovider|myuser.

And that’s it: converting the login names to claims and vice versa is that simple. And using the DecodeClaim and EncodeClaim methods makes it more reliable than parsing or building the strings manually.

Programmatically Convert Office Documents (.docx, .pptx, .pub) into PDF

Word

========

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Office.Interop.Word;

 

namespace DocConverter

{

class Program

{

static void Main(string[] args)

{

ApplicationClass wordApplication = new ApplicationClass();

 

Document wordDocument = null;

object paramSourceDocPath = @"D:\Fun\Test.docx";

object paramMissing = Type.Missing;

string paramExportFilePath = @"D:\Fun\Test.pdf";

WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;

bool paramOpenAfterExport = false;

WdExportOptimizeFor paramExportOptimizeFor =

WdExportOptimizeFor.wdExportOptimizeForPrint;

WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;

int paramStartPage = 0;

int paramEndPage = 0;

WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;

bool paramIncludeDocProps = true;

bool paramKeepIRM = true;

WdExportCreateBookmarks paramCreateBookmarks =

WdExportCreateBookmarks.wdExportCreateWordBookmarks;

bool paramDocStructureTags = true;

bool paramBitmapMissingFonts = true;

bool paramUseISO19005_1 = false;

try

{

// Open the source document.

wordDocument = wordApplication.Documents.Open(

ref paramSourceDocPath, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing, ref paramMissing, ref paramMissing,

ref paramMissing);

 

// Export it in the specified format.

if (wordDocument != null)

wordDocument.ExportAsFixedFormat(paramExportFilePath,

paramExportFormat, paramOpenAfterExport,

paramExportOptimizeFor, paramExportRange, paramStartPage,

paramEndPage, paramExportItem, paramIncludeDocProps,

paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,

paramBitmapMissingFonts, paramUseISO19005_1,

ref paramMissing);

}

catch (Exception ex)

{

// Respond to the error

}

finally

{

// Close and release the Document object.

if (wordDocument != null)

{

wordDocument.Close(ref paramMissing, ref paramMissing,

ref paramMissing);

wordDocument = null;

}

 

// Quit Word and release the ApplicationClass object.

if (wordApplication != null)

{

wordApplication.Quit(ref paramMissing, ref paramMissing,

ref paramMissing);

wordApplication = null;

}

 

GC.Collect();

GC.WaitForPendingFinalizers();

GC.Collect();

GC.WaitForPendingFinalizers();

}

 

}

}

}

 

PowerPoint

=============

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Office.Interop.PowerPoint;

namespace PowerPointConverter

{

class Program

{

static void Main(string[] args)

{

Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();

Microsoft.Office.Interop.PowerPoint.Presentation presentation = ppApp.Presentations.Open(@"D:\Fun\Test.pptx", Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);

presentation.ExportAsFixedFormat(@"D:\Fun\Test.xps",

PpFixedFormatType.ppFixedFormatTypeXPS,

PpFixedFormatIntent.ppFixedFormatIntentPrint,

Microsoft.Office.Core.MsoTriState.msoFalse,

PpPrintHandoutOrder.ppPrintHandoutHorizontalFirst,

PpPrintOutputType.ppPrintOutputSlides,

Microsoft.Office.Core.MsoTriState.msoFalse,

null,

PpPrintRangeType.ppPrintAll,

"",

false,

false,

false,

true,

true,

System.Reflection.Missing.Value);

presentation.Close();

presentation = null;

ppApp = null;

GC.Collect();

 

}

}

}

 

Publisher
===============

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Office.Interop.Publisher;

using System.Reflection;

 

namespace PublisherConverter

{

class Program

{

static void Main(string[] args)

{

Microsoft.Office.Interop.Publisher.Application pbApp = new Microsoft.Office.Interop.Publisher.Application();

Microsoft.Office.Interop.Publisher.Document pbDoc = pbApp.Open(@"D:\Fun\Test4.pub", true, true, PbSaveOptions.pbDoNotSaveChanges);

pbDoc.ExportAsFixedFormat(PbFixedFormatType.pbFixedFormatTypePDF, @"D:\Fun\Test.pdf", PbFixedFormatIntent.pbIntentPrinting, true, 300, 450, 1200, 1800, 1, pbDoc.Pages.Count, 1, false, PbPrintStyle.pbPrintStyleDefault, false, false, false, System.Reflection.Missing.Value);

pbDoc.Close();

pbDoc = null;

pbApp = null;

GC.Collect();

}

}

}

How to programmatically attach a document or file in a SPListItem



How to programmatically attach a document or file in a SPListItem of SPList (SharePoint's List).

Following method contains the logic to attach the document / file (codes with comments are self-explainatory).
       /// <summary>
    /// Programmatically Attach document in SPDocument Library 
/// </summary>
void documentAttachmentInSPList()
{
//SPListItem spListItem = null; //write code to get the SPListItem.
//fileUpload is control ID of ASP.Net FileUpload control
if (!string.IsNullOrEmpty(fileUpload.FileName))
{
//Get file extension ( *.doc OR *.docx )
string fileExtension
= fileUpload.FileName.Substring(fileUpload.FileName.IndexOf("."));

//FILENAME is file name visible in SPListItem
//Check file is already added or not. If added then delete it.
for (int i = 0; i < spListItem.Attachments.Count; i++)
{
if ((spListItem.Attachments[i] != null)
&& (spListItem.Attachments[i].Equals("FILENAME" + fileExtension)))
{
spListItem.Attachments.Delete("FILENAME" + fileExtension);
break;
}
}

//Attach the file.
spListItem.Attachments.Add("FILENAME" + fileExtension, fileUpload.FileBytes);

//LISTNAME is SPList's name
//See the attached file as link in user-created custom column.
string attachmentURL = Request.Url.Scheme + "://" + Request.Url.Authority
+ "/Lists/" + "LISTNAME" + "/Attachments/" + spListItem.ID + "/";

spListItem["Attached File"]
= attachmentURL + "FILENAME" + fileExtension + ", View FILE";
}
}


Add User To SharePoint Group Programmatically

Let’s see how we can programmatically add a user to existing SharePoint user group . You can use the following method which has two string parameters, one for group name and other for user login name for get the above task done.










public void addUserToGroup(string groupName, string userLoginName)

{

    string strUrl = "http://mysite:5050/";

    using (SPSite site = new SPSite(strUrl))

    {

        using (SPWeb web = site.OpenWeb())

        {

            try

            {

                web.AllowUnsafeUpdates = true;

                SPUser spUser = web.EnsureUser[userLoginName];


                if (spUser != null)

                {

                    SPGroup spGroup = web.Groups[groupName];

                    if (spGroup != null)

                    spGroup.AddUser(spUser);

                }

            }

            catch (Exception ex)

            {

                throw ex;

            }

            finally

            {

                web.AllowUnsafeUpdates = false;

            }

        }

    }

}




Here you should provide the login name in correct format (DomainName\UserName). For example you can call this method like follow,

addUserToGroup("Test Group", "SA\\codeitsimple");