Friday, 3 July 2015

How to get current year and current month get selected in SharePoint column

I have added Year column in SharePoint list and I want the default value to be current year.
To do this in default value of a column, select calculate value and put below formula in Text box

=TEXT(Today,"YYYY")

Similarly to select current month in moth column put below formula in Text box:

=TEXT(Today,"mmmm")

Hope this helps!!!!

Working with Checkbox in SharePoint

In below code we will see, how to add multichoice chekcbox field in list, add item with checkbox field and get value from chekcbox field.

using System;
using System.Collections.Specialized;
using Microsoft.SharePoint;

namespace ConsoleApp
{
    class Program
    {
        static string listTitle = "My Custom List";
        static string fieldTitle = "Gift Options";
        static string fieldInternalName = null;

        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.RootWeb)
                {
                    SPList list = web.Lists.TryGetList(listTitle);
                    if (list != null)
                    {
                        // Add a multichoice field to the list.
                        StringCollection choices = new StringCollection();
                        choices.Add("Gift wrap");
                        choices.Add("Gift card");
                        choices.Add("Include gift receipt");

                        fieldInternalName = list.Fields.Add(fieldTitle, SPFieldType.MultiChoice, false, false, choices);
                        list.Update();

                        // Get a reference to the field.
                        SPFieldMultiChoice choiceField = (SPFieldMultiChoice)list.Fields.GetField(fieldInternalName);

                        // Create a field value with all choices selected.
                        // (A CheckBoxChoiceField control would have all boxes checked.)
                        SPFieldMultiChoiceValue values = new SPFieldMultiChoiceValue();
                        foreach (string choice in choices)
                        {
                            values.Add(choice);
                        }

                        // Add an item to the list.
                        SPListItem item = list.Items.Add();
                        item[SPBuiltInFieldId.Title] = "Test item";
                        item[choiceField.Id] = values;
                        item.Update();

                        // Get the value of the field in the item.
                        string rawValue = item[choiceField.Id].ToString();
                        SPFieldMultiChoiceValue typedValue = new SPFieldMultiChoiceValue(rawValue);

                        // Print the value.
                        Console.WriteLine("The raw value is {0}", rawValue);
                        Console.WriteLine("The value delimiter is {0}", SPFieldMultiChoiceValue.Delimiter);
                        for (int i = 0; i < typedValue.Count; i++)
                        {
                            Console.WriteLine("The value at index {0} is {1}", i, typedValue[i]);
                        }
                    }
                }
            }
            Console.WriteLine("\nPress ENTER to continue....");
            Console.Read();
        }
    }
}

Hope this helps!!!









How to download all deployed wsp solution files in SharePoint

Usually SharePoint admin guys takes backup of content DB and all physical files if they want to create replica of existing SharePoint site when actual site is down.

But that will not complete solves the solution, if there are any custom solutions already deployed.

So, admin guys needs those solution files.
To get those solution files which are deployed in SharePoint use the following power shell command to get all deployed solution files.

$pathName = "<PATH to Save files>"
foreach ($solution in Get-SPSolution)   
{
     $solid = $Solution.SolutionID
     $title = $Solution.Name
     $filename = $Solution.SolutionFile.Name
     $solution.SolutionFile.SaveAs("$pathName\$filename") 
}

Ref: http://ukreddysharepoint2010.blogspot.in/2015/05/how-to-download-all-deployed-wsp.html

Hope this helps!!!

When to use BeforeProperties, AfterProperties, properties.ListItem in event receiver

Below table give us the idea on when to use BeforeProperties, AfterProperties, properties.ListItem in event receiver

Library
BeforeProperties
AfterProperties
properties.ListItem
ItemAdding
No value
No value
Null
ItemAdded
No value
No value
New value
ItemUpdating
Original value
Changed value
Original value
ItemUpdated
Original value
Changed value
Changed value
ItemDeleting
No value
No value
Original value
ItemDeleted
No value
No value
Null

Hope this helps!!!

How to find out SPUser belongs to Specific Group

How can I find out programmatically if current user belongs to some group on sharepoint website?

We can use below code:

SPWeb site = SPContext.Current.Web;
SPGroup managerGroup = site.Groups["Your_Group_Name"];

bool isManager = site.IsCurrentUserMemberOfGroup(managerGroup.ID);

Hope this helps!!!!
Related Posts Plugin for WordPress, Blogger...