Tuesday, May 21, 2013

How to use a List Definition for Sharepoint 2010 in VS 2010

When creating a list definition you have to create and edit two main files. They are the Elements.xml and Schema.xml. In the Elements.xml you have to add the <Fields> and <Content Type> elements. Then in the Schema.xml you have to refer them.

When creating the GUIDs for the Fields and Content types, you have to be careful because they have to be inherited. Then those GUIDs can be created using Tools-> Create GUID-> New-> 4th Option

This is the MSDN reference for creating a List Definition.
List Instances are the objects that can be used for creating instances of the list definition.

Update:

You can only change the schema.xml to create the List Definition. There first you have to add the <Content Type> then the <Fields> and <FiledRef>s. After that, in the <ViewFields> you can add the <FieldRefs> to determine, which <Fileds> should be viewed.
When creating the GUID for the content type ID you have to be careful. There you have to add the correct Item ID (You can find them in  C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\ctypes\ctypes.xml).

To create the ID for the Content Type, first add the ItemID (Eg: 0x01 for ContentTypes) then add the GUID without the hyphens. For FieldRef IDs also there are default IDs for fields like Title. They can be found in the wss xmls in the 14\TEMPLATE\FEATURES folder. As an example, field ID for the title field is fa564e0f-0c70-4ab9-b863-0177e6ddd247.

Update:

Filed Type IDs are here:


C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\fields\fieldswss3.xml

Issues and Solutions for Sharepoint 2010 - Enable Session State Issue

This issue arises due to missing or removing session element. For that just verify whether the "enableSessionstate" is set to "true" and add the following line in the httpmodules in web.config file.

1) <pages enableSessionState="true" enableViewState="true" enableViewStateMac="true"/>
2) <add name="Session" type="System.Web.SessionState.SessionStateModule" />

If this doesn't work check whether "Session" is removed somewhere like this,
<remove name="Session" />.

If so add the 2nd line again after that. 
For a detailed article visit this blog. 

Monday, May 20, 2013

Sending an Email using Sharepoint Web Part


For sending a mail using SPUtility I referred to this article. This code is just a demo.

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Collections;
using Microsoft.SharePoint.Utilities;

namespace CustomPeoplePickerOne.PeoplePicker_swp_1
{
    [ToolboxItemAttribute(false)]
    public class PeoplePicker_swp_1 : WebPart
    {
        PeopleEditor pe = null;
        SPWeb web = null;
        protected override void CreateChildControls()
        {
            web = SPContext.Current.Web;

            pe = new PeopleEditor();
            pe.ValidatorEnabled = true;
            this.Controls.Add(pe);

            Button viewPeople = new Button();
            viewPeople.Text = "View";
            viewPeople.Click += new EventHandler(viewPeople_Click);
            this.Controls.Add(viewPeople);

            Button sendMailButton = new Button();
            sendMailButton.Text = "Send Mail";
            sendMailButton.Click += new EventHandler(sendMailButton_Click);
            this.Controls.Add(sendMailButton);
               
        }

        void sendMailButton_Click(object sender, EventArgs e)
        {

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {

                if (SendMail("kinathr@navantis.com"))
                {
                    this.Controls.Add(new LiteralControl("<br/>"));
                    this.Controls.Add(new LiteralControl("<b>Email Successfully sent to Kinath</b>"));
                }
                else
                {
                    this.Controls.Add(new LiteralControl("<br/>"));
                    this.Controls.Add(new LiteralControl("<b>Sending Failed to Kinath</b>"));
                }
             
            });


        }

        void viewPeople_Click(object sender, EventArgs e)
        {
            viewPeopleOption4();
        }

        void viewPeopleOption()
        {
            foreach (var loginName in pe.CommaSeparatedAccounts.Split(';', ','))
            {
                SPUser user = SPContext.Current.Web.EnsureUser(loginName);
                TextBox tb = new TextBox();
                tb.Text = string.Format("Name : {0} Email : {1}", user.Name, user.Email);
                this.Controls.Add(tb);

                //SPUtility.SendEmail(
            }
        }

        void viewPeopleOption2()
        {
            //Create the data table and add data to it
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn(" Name ", typeof(String)));
            dt.Columns.Add(new DataColumn(" Email ", typeof(String)));

            //get data from the people editor
            for (int i = 0; i < pe.ResolvedEntities.Count; i++)
            {
                string accName = "";
                string email = "";
                PickerEntity pickerEntity = (PickerEntity)pe.ResolvedEntities[i];
                Hashtable hsEntityData = pickerEntity.EntityData;
                if (hsEntityData.ContainsKey("Account Name"))
                {
                    accName = Convert.ToString(hsEntityData["Account Name"]);
                }
                else
                {
                    accName = "Not available";
                }
                if (hsEntityData.ContainsKey("Email"))
                {
                    email = Convert.ToString(hsEntityData["Email"]);
                }
                else
                {
                    email = "Not available";
                }


                TextBox tb = new TextBox();
                tb.Text = string.Format("Name : {0} Email : {1}", accName, email);
                this.Controls.Add(tb);

            }
        }

        void viewPeopleOption3()
        {
            var accountName = pe.Accounts[0];
            SPUser user = web.EnsureUser((string)accountName);

            Label userLable = new Label();
            userLable.Text = string.Format("User : {0} Email : {1}",user.Name,user.Email);
            this.Controls.Add(userLable);

        }

        void viewPeopleOption4()
        {
            if (pe.IsValid)
            {
                for (int i = 0; i < pe.ResolvedEntities.Count; i++)
                {
                    PickerEntity picker = (PickerEntity)pe.ResolvedEntities[i];
                    Hashtable hstEntityData = picker.EntityData;
                    string[] name = picker.DisplayText.Split(' ');
                    string Email = Convert.ToString(hstEntityData["Email"]);
                    //string FName = name[0].ToString();
                    //string SName = (name[1].ToString() != null) ? name[1].ToString() : "";
                    Label userLable = new Label();
                    userLable.Text = Email;
                    this.Controls.Add(userLable);

                }
            }
        }

        bool SendMail(string userMail)
        {
            bool success = false;
            /*
             * public static bool SendEmail(
                       SPWeb web,
                       bool fAppendHtmlTag,
                       bool fHtmlEncode,
                       string to,
                       string subject,
                       string htmlBody,
                       bool appendFooter
                        )
             * */

            SPWeb web = SPContext.Current.Web;
            bool fAppendHtmlTag = false;
            bool fHtmlEncode = false;
            string toEmail = userMail;
            string subject = "Test Email";
            string htmlBody = "Test Email Body";
            bool appendFooter = false;

            //first check whehter the SMTP settings are established
            if (SPUtility.IsEmailServerSet(web))
            {
                success = SPUtility.SendEmail(web, fAppendHtmlTag, fHtmlEncode, toEmail, subject, htmlBody, appendFooter);
            }
            else
            {
                this.Controls.Add(new LiteralControl("<br/>"));
                this.Controls.Add(new LiteralControl("<b>SMTP settings not set</b>"));
            }
            return success;
        }
    }
}


How to Get User Emails from Sharepoint 2010 using People Editor

This code shows few options on selecting users from sharepoint site and retrieving their mails using people editor web control

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Collections;

namespace CustomPeoplePickerOne.PeoplePicker_swp_1
{
    [ToolboxItemAttribute(false)]
    public class PeoplePicker_swp_1 : WebPart
    {
        PeopleEditor pe = null;
        SPWeb web = null;
        protected override void CreateChildControls()
        {
            web = SPContext.Current.Web;

            pe = new PeopleEditor();
            pe.ValidatorEnabled = true;
            this.Controls.Add(pe);

            Button viewPeople = new Button();
            viewPeople.Text = "View";
            viewPeople.Click += new EventHandler(viewPeople_Click);
            this.Controls.Add(viewPeople);
                 
        }

        void viewPeople_Click(object sender, EventArgs e)
        {
            viewPeopleOption4();
        }

        void viewPeopleOption() 
        {
            foreach (var loginName in pe.CommaSeparatedAccounts.Split(';', ','))
            {
                SPUser user = SPContext.Current.Web.EnsureUser(loginName);
                TextBox tb = new TextBox();
                tb.Text = string.Format("Name : {0} Email : {1}", user.Name, user.Email);
                this.Controls.Add(tb);
            }
        }

        void viewPeopleOption2() 
        {
            //Create the data table and add data to it
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn(" Name ", typeof(String)));
            dt.Columns.Add(new DataColumn(" Email ", typeof(String)));

            //get data from the people editor
            for (int i = 0; i < pe.ResolvedEntities.Count; i++)
            {
                string accName = "";
                string email = "";
                PickerEntity pickerEntity = (PickerEntity)pe.ResolvedEntities[i];
                Hashtable hsEntityData = pickerEntity.EntityData;
                if (hsEntityData.ContainsKey("Account Name"))
                {
                    accName = Convert.ToString(hsEntityData["Account Name"]);
                }
                else
                {
                    accName = "Not available";
                }
                if (hsEntityData.ContainsKey("Email"))
                {
                    email = Convert.ToString(hsEntityData["Email"]);
                }
                else
                {
                    email = "Not available";
                }


                TextBox tb = new TextBox();
                tb.Text = string.Format("Name : {0} Email : {1}", accName, email);
                this.Controls.Add(tb);

            }
        }

        void viewPeopleOption3() 
        {
            var accountName = pe.Accounts[0];
            SPUser user = web.EnsureUser((string)accountName);

            Label userLable = new Label();
            userLable.Text = string.Format("User : {0} Email : {1}",user.Name,user.Email);
            this.Controls.Add(userLable);

        }

        void viewPeopleOption4() 
        {
            if (pe.IsValid)
            {
                for (int i = 0; i < pe.ResolvedEntities.Count; i++)
                {
                    PickerEntity picker = (PickerEntity)pe.ResolvedEntities[i];
                    Hashtable hstEntityData = picker.EntityData;
                    string[] name = picker.DisplayText.Split(' ');
                    string Email = Convert.ToString(hstEntityData["Email"]);
                    //string FName = name[0].ToString();
                    //string SName = (name[1].ToString() != null) ? name[1].ToString() : "";
                    Label userLable = new Label();
                    userLable.Text = Email;
                    this.Controls.Add(userLable);

                }
            }
        }
    }
}