Sunday, February 24, 2013

Adding Active directory group in SharePoint group

This code will help you to add the Active Directory Group in the Custom SharePoint Group. Consider the situation you need to give the access permission to multiple users on the basis of department wise or any category based which is already created in AD. Now add this group in SharePoint group to simplify the process. This will add the new sharepoint group programmatically and the AD group in to it.

           SPWeb web = SPContext.Current.Web;
            web.AllowUnsafeUpdates = true;
            web.SiteGroups.Add("Champion10", web.CurrentUser, web.CurrentUser, string.Empty);
         
            SPGroup group= web.SiteGroups["Champion10"];
            SPRoleAssignment roleAssignment = new SPRoleAssignment(group);
            SPRoleDefinition roleDefinition = web.RoleDefinitions["Full Control"];
            roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
            web.RoleAssignments.Add(roleAssignment);


            SPUser AdGroup = web.EnsureUser("MurugesanAD");
           //This line will do the trick.You can also ensure by using the  bool isAD=AdGroup.IsDomainGroup

            group.AddUser(g);

       
            web.Update();
            web.AllowUnsafeUpdates = false;
         
            Literal1.Text = "Done";

Sunday, February 17, 2013

Reminder Workflow in SharePoint Designer

For sending the reminder email notifications to the workflow participant,I would prepare to create a separate workflow as I do not want to disturb the business logic in core workflow. In main list,I have associated the core workflow and The secondary workflow for sending the reminder email. When the core workflow sending by default email,I simply capture the some of its columns in another list along with "ReminderCount". In my new list "Reminder",column reminder count will be set to 1 and then some pause until some defined timeline. Activity 2 : [Create Item] It will create a list item in the list "Reminder" as soon as core workflow triggered. In output to variable workflow will create the variable [UpdatedId - I just renamed] which is used to store the item id of the "Reminder" This ID will be used for updating the ReminderCount in this list later. Activity 3: I just logged the message for ensure the newly created item id in the "Reminder" List. Activity 4: Pausing for some time to get response from approver. If approver didn't respond to his task within time. Activity 5 Here I do calculation for reminder count.Just retrieve previous the value of the "Reminder Count" column and adding to 1. For retrieving the previous value of "Reminder Count" I simply used the List ID and its value from the workflow created list item id. Activity 6 : Using the Update List Item,for this I just updating the item [Reminder Count] by passing the workflow created item id (UpdatedId) and its value which is calculated from the previous activity (calc1).

Monday, February 11, 2013

OpenXML-Update excel cell value

Code snippet for helping to update the specific cell in excel using the OpenXML SDK 2.0. When you updates the specific cell make sure to test with isNull or Empty otherwise write some default value in the sell and then use the below code to update. Because OpenXML ignores the empty cell.

 using (SpreadsheetDocument document = SpreadsheetDocument.Open("D:\\template.xlsx", true))
            {
                WorkbookPart wbPart = document.WorkbookPart;
                Sheet theSheet = wbPart.Workbook.Descendants().
                Where(s => s.Name == "Sheet1").FirstOrDefault();
                Worksheet ws = ((WorksheetPart)(wbPart.GetPartById(theSheet.Id))).Worksheet;
                WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));
                SheetData sheetData = ws.GetFirstChild();
                var theRow = sheetData.Elements(). Where(r => r.RowIndex.Value == 6).FirstOrDefault();
                Cell refCell = theRow.Elements().
                Where(c => c.CellReference == "G6").FirstOrDefault();
                refCell.CellValue = new CellValue("TestComes");
                refCell.DataType = new EnumValue(CellValues.String);
                ws.Save();
           
            }

Thursday, February 07, 2013

FileUpload SharePoint Online

Recently I was asked to workaround for uploading the files to SharePoint list pro-grammatically for SharePoint online. As I can use only sandbox solution,I tried the SPProxyOperation class to upload and save the file. But its very complex process for staging the package and deployment. Later I come up with very simple approach,After adding the items to the list I retrieved the Item ID and ListID and passing these parameters to the file "/_layouts/AttachFile.asp?ListID="{GUID}"&ItemID=itemId&isDlg=1 into SPModal dialog.

Click here to upload file to this item