Saturday, 25 February 2012

Programmatically send email using console application


 using (SPSite osite = new SPSite("http://igrid102:1974"))
            {
                using (SPWeb oweb = osite.OpenWeb())
                {
                    try
                    {
                        StringDictionary headers = new StringDictionary();
                        headers.Add("from","system@domain.com");
                        headers.Add("to", "anandraj@triadinfotech.com");
                        headers.Add("subject", "Welcome to the SharePoint group: ABC site: ");
                        headers.Add("content-type", "text/html"); //This is the default type
                        System.Text.StringBuilder strMessage = new System.Text.StringBuilder();
                        strMessage.Append("<br><br><b>Login Instructions: </b><br>");
                        strMessage.Append("<font color='red'><UL><li>If you are a US employee ALWAYS login in using the following login format </font><br>");
                        SPUtility.SendEmail(oweb, headers, strMessage.ToString());
                        Console.WriteLine("Mail send sucessfully");

                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                }
            }

Manikandan blog

Saturday, 18 February 2012

Padmaraj blog

Configuring FBA

deleting item in shared document and move to all subsites shared document files


public override void ItemDeleting(SPItemEventProperties properties)
        {
            if (properties.Web.IsRootWeb)
            {
                if (properties.ListTitle == "Shared Documents")
                {
                    SPListItem oitem = properties.ListItem;
                    string site = properties.Web.Url;
                    using (SPSite osite = new SPSite(site))
                    {
                        try
                        {
                            SPWebCollection owebcol = osite.AllWebs;
                            foreach (SPWeb oweb in owebcol)
                            {
                                if (!oweb.IsRootWeb)
                                {
                                    oweb.AllowUnsafeUpdates = true;
                                    SPList olistnew = oweb.Lists.TryGetList("Shared Documents");
                                    byte[] filebyte = oitem.File.OpenBinary();
                                    string desturl = olistnew.RootFolder.Url + "/" + oitem.File.Name;
                                    SPFile fileadd = olistnew.RootFolder.Files.Add(desturl, filebyte, true);
                                    oweb.AllowUnsafeUpdates = false;

                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                }
            }
       }


    }
}

Transfer document from one library to another


 SPSecurity.RunWithElevatedPrivileges(delegate()
                                    {
                                        using (SPSite osite = new SPSite("http://admin-pc:2345"))
                                        {
                                            try
                                            {
                                                using (SPWeb oweb = osite.OpenWeb("subsite"))
                                                {
                                                    SPDocumentLibrary olist = (SPDocumentLibrary)oweb.Lists.TryGetList("Shared Documents");
                                                    using (SPWeb newweb = osite.RootWeb)
                                                    {
                                                        SPDocumentLibrary olistnew = (SPDocumentLibrary)newweb.Lists.TryGetList("Shared Documents");
                                                        int count = olistnew.Items.Count;
                                                        int i;

                                                        for (i = 0; i < count; i++)
                                                        {

                                                            oweb.AllowUnsafeUpdates = true;
                                                            SPListItem oitemnew = olistnew.Items[i];
                                                            byte[] fileBytes = oitemnew.File.OpenBinary();
                                                            string desturl = olist.RootFolder.Url + "/" + oitemnew.File.Name;
                                                            SPFile librarynew = olist.RootFolder.Files.Add(desturl, fileBytes, true);
                                                            librarynew.Update();
                                                            oweb.AllowUnsafeUpdates = false;

                                                        }
                                                        Console.WriteLine("Document transfered");
                                                        Console.ReadLine();

                                                    }
                                                }
                                            }
                                            catch (Exception ex)
                                            {
                                                throw ex;
                                            }
                                        }
                                    });
        }

Friday, 17 February 2012

Create document set content type programmatically



            using (SPSite osite = new SPSite("http://admin-pc:2345") )
            {
                using (SPWeb oweb = osite.OpenWeb())
                {
                    try
                    {
                        SPContentType contype = oweb.ContentTypes.Add(new SPContentType(oweb.ContentTypes["Documentset"], oweb.ContentTypes, "My Created Content typenew by anand"));
                        contype.Group = "my new group";
                        DocumentSetTemplate newdocumentsettemplate = DocumentSetTemplate.GetDocumentSetTemplate(contype);
                        newdocumentsettemplate.AllowedContentTypes.Add(oweb.ContentTypes["Document"].Id);
                        newdocumentsettemplate.SharedFields.Add(oweb.Fields["Title"]);
                        newdocumentsettemplate.Update(true);
                        contype.Update();
                        oweb.AllowUnsafeUpdates = true;
                        oweb.Update();
                        oweb.AllowUnsafeUpdates = false;
                        Console.WriteLine("Document set created");
                        Console.ReadLine();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

Thursday, 16 February 2012

list item deleting event


try
{ 
   properties.Cancel = true;
   properties.ErrorMessage = "Bugs can only be resolved not deleted!";
}
catch (Exception ex)
{
   return;
}
finally
{
   this.EventFiringEnabled = true;
}

list item deleted


  public override void ItemDeleted(SPItemEventProperties properties)
       {
           base.ItemDeleted(properties);
           if (properties.ListTitle == "anand")
           {  
               string site = properties.Web.Url;
               using (SPSite osite = new SPSite(site))
               {
                   using (SPWeb oweb = osite.OpenWeb())
                   {
                       SPList olist = oweb.Lists.TryGetList("Training list final123");
                       SPListItem oitem = olist.Items.Add();
                       oitem["Title"] = "anandNew1";
                       oweb.AllowUnsafeUpdates = true;
                       oitem.Update();
                       oweb.AllowUnsafeUpdates = false;
                   }
               }
           }


       }

List item added


public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            try
            {
                if (properties.ListTitle == "anand")
                {
                    SPListItem oitem = properties.ListItem;
                    using (SPSite osite = new SPSite(oitem.Web.Url))
                    {
                        SPWebCollection owebcol = osite.AllWebs;
                        SPWeb oweb = owebcol["subsite"];
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            SPList olist = oweb.Lists.TryGetList("listnew");
                            SPListItem oitemnew = olist.Items.Add();
                            oitemnew["Title"] = "anandnew";
                            oweb.AllowUnsafeUpdates = true;
                            this.EventFiringEnabled = false;
                            oitemnew.Update();
                            oweb.AllowUnsafeUpdates = false;
                            this.EventFiringEnabled = true;
                        });


                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

find lists by their starting letter


using (SPSite osite = new SPSite("http://admin-pc:2345"))
            {
                using (SPWeb oweb = osite.OpenWeb())
                {
                    SPListCollection olistcol = oweb.Lists;
                    foreach (SPList olist in olistcol)
                    {
                        if (olist.Title.StartsWith("T"))
                        {
                           Console.WriteLine(olist.Title);
                        }
                    }
                    Console.ReadLine();

                }
            }

Add list item for particular site


using (SPSite osite = new SPSite("http://admin-pc:2345"))
            {
                SPWebCollection owebcol = osite.AllWebs;
                {
                    SPWeb oweb = owebcol["subsite"];
                    SPList olist = oweb.Lists.TryGetList("listnew");
                    SPListItem oitem = olist.Items.Add();
                    oitem["Title"] = "anand";
                    oitem.Update();
                    Console.WriteLine("sucess");
                    Console.ReadLine();

                }
            }

Add look up field programmatically


 using (SPSite osite = new SPSite("http://admin-pc:2345"))
            {
                using (SPWeb oweb = osite.OpenWeb())
                {
                    try
                    {
                        SPList olist = oweb.Lists.TryGetList("anand");
                        SPList olistnew = oweb.Lists.TryGetList("Training");
                        olist.Fields.AddLookup("lookupfield", olistnew.ID, false);
                        SPFieldLookup lookup = olist.Fields["lookupfield"] as SPFieldLookup;
                        lookup.LookupField = olistnew.Fields[SPBuiltInFieldId.Title].InternalName;
                        SPView newview = olist.DefaultView;
                        newview.ViewFields.Add("lookupfield");
                        oweb.AllowUnsafeUpdates = true;
                        newview.Update();
                        lookup.Update();
                        oweb.AllowUnsafeUpdates = false;
                        Console.WriteLine("operation sucess");
                        Console.ReadLine();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }

Create list and add fields


 public static void createlist()
        {
            string site = "http://admin-pc:2345";
            try
            {  
                using (SPSite osite = new SPSite(site))
                {
                    using (SPWeb oweb = osite.OpenWeb())
                    {
                        SPListTemplate template = oweb.ListTemplates["Custom List"];

                        if (oweb.Lists.TryGetList("Training list final123")==null)
                        {
                            oweb.AllowUnsafeUpdates = true;
                            oweb.Lists.Add("Training list final123", " ", template);
                            SPList olist = oweb.Lists.TryGetList("Training list final123");
                            olist.Fields.Add("field1", SPFieldType.Text, false);
                            olist.Fields.Add("field2", SPFieldType.Text, false);
                            olist.Fields.Add("field3", SPFieldType.Choice, false);
                            SPFieldChoice ochoice = (SPFieldChoice)olist.Fields["field3"];
                            ochoice.EditFormat=SPChoiceFormatType.Dropdown;
                            ochoice.Choices.Add("Male");
                            ochoice.Choices.Add("female");
                            ochoice.Update();

                            olist.OnQuickLaunch = true;
                            SPView newview = olist.DefaultView;
                            newview.ViewFields.Add("field1");
                            newview.ViewFields.Add("field2");
                            newview.ViewFields.Add("field3");
                            newview.Update();
                            olist.Update();
                            oweb.AllowUnsafeUpdates = false;
                            Console.WriteLine("List fields added");
                        }
                        else
                        {
                            SPSecurity.RunWithElevatedPrivileges(delegate()
                                {
                                    using (SPSite osite1 = new SPSite(site))
                                    {
                                        using (SPWeb oweb1 = osite1.OpenWeb())
                                        {

                                            SPList olist = oweb.Lists.TryGetList("Training list final123");
                                            olist.Fields.Delete("field1");
                                            olist.Fields.Delete("field2");
                                            olist.Fields.Delete("field3");
                                            olist.Fields.Add("field1", SPFieldType.Text, false);
                                            olist.Fields.Add("field2", SPFieldType.Text, false);
                                            olist.Fields.Add("field3", SPFieldType.Choice, false);
                                            SPFieldChoice ochoice = (SPFieldChoice)olist.Fields["field3"];
                                            ochoice.EditFormat=SPChoiceFormatType.RadioButtons;
                                            ochoice.Choices.Add("Male");
                                            ochoice.Choices.Add("female");
                                            ochoice.Update();
                                            SPView newview = olist.DefaultView;
                                            newview.ViewFields.Add("field1");
                                            newview.ViewFields.Add("field2");
                                            newview.ViewFields.Add("field3");
                                            newview.Update();
                                            olist.OnQuickLaunch = true;
                                            oweb.AllowUnsafeUpdates = true;
                                            olist.Update();
                                            oweb.AllowUnsafeUpdates = false;

                                        }
                                    }
                                });
                            Console.WriteLine("List already Exist it has to be udgraded");
                         
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            Console.ReadLine();

        }

Create a choice field columns


 using (SPSite osite = new SPSite("http://admin-pc:2345"))
            {
                using (SPWeb oweb = osite.OpenWeb())
                {
                    try
                    {
                        SPList olist = oweb.Lists.TryGetList("anand");
                        olist.Fields.Add("new1", SPFieldType.Choice, false);
                        SPFieldChoice ochoice = (SPFieldChoice)olist.Fields["new1"];
                        ochoice.EditFormat = SPChoiceFormatType.Dropdown;
                        ochoice.Choices.Add("Male");
                        ochoice.Choices.Add("Female");
                        ochoice.Update();
                        SPView view = olist.DefaultView;
                        view.ViewFields.Add("new1");
                        view.Update();
                        oweb.AllowUnsafeUpdates = true;
                        olist.Update();
                        oweb.AllowUnsafeUpdates = false;
                        Console.WriteLine("operation sucess");
                        Console.ReadLine();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                }
            }

Monday, 13 February 2012

Uploading document programmatically


 static void Main(string[] args)
        {
            string filepath = @"D:\KA_AllFiles\Training Materials\Wipro Training\Infopath Form.docx";
            string libraryname = "Shared Documents";
            using (SPSite osite = new SPSite("http://admin-pc:2345"))
            {
                using (SPWeb oweb = osite.OpenWeb())
                {
                    if (System.IO.File.Exists(filepath))
                    {  
                        SPFolder myLibrary = oweb.Folders[libraryname];
                        String fileName = System.IO.Path.GetFileName(filepath);
                        FileStream fileStream = File.OpenRead(filepath);
                        SPFile spfile = myLibrary.Files.Add(fileName, fileStream, true);
                        myLibrary.Update();
                    }
                    else
                    {
                        throw new FileNotFoundException("file not found");
                    }
                }
            }

        }

Create list programmatically using console application


 public static void createlist()
        {
            try
            {
                using (SPSite osite = new SPSite("http://admin-pc:2345"))
                {
                    using (SPWeb oweb = osite.OpenWeb())
                    {
                        SPListTemplate template = oweb.ListTemplates["Custom List"];

                        if (oweb.Lists.TryGetList("Training list new") == null)
                        {
                            oweb.AllowUnsafeUpdates = true;
                            oweb.Lists.Add("Training list new", " ", template);
                            SPList olist = oweb.Lists.TryGetList("Training list new");
                            olist.Fields.Add("field1", SPFieldType.Text, false);
                            olist.Fields.Add("field2", SPFieldType.Text, false);
                            olist.OnQuickLaunch = true;
                            SPView newview = olist.DefaultView;
                            newview.ViewFields.Add("field1");
                            newview.ViewFields.Add("field2");
                            newview.Update();
                            olist.Update();
                            oweb.AllowUnsafeUpdates = false;
                            Console.WriteLine("List fields added");
                        }
                        else
                        {
                            Console.WriteLine("List already Exist");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            Console.ReadLine();

        }
    }

Create list programmatically


public void createlist()
        {
            try
            {
                using (SPSite osite = new SPSite(SPContext.Current.Web.Site.ID))
                {
                    using (SPWeb oweb = osite.OpenWeb())
                    {
                        oweb.AllowUnsafeUpdates = true;
                        if (oweb.Lists.TryGetList("Training list") == null)
                        {
                            oweb.Lists.Add("Training list", " ", SPListTemplateType.GenericList);
                        }
                        SPList olist = oweb.Lists.TryGetList("Training list");
                        olist.OnQuickLaunch = true;
                        olist.Update();
                        oweb.AllowUnsafeUpdates = false;
                     


                    }

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

Friday, 10 February 2012

ADDING LIST ITEM


 public partial class listdataUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            listadd();
        }
        public void listadd()
        {
            using (SPSite osite = new SPSite(SPContext.Current.Web.Site.ID))
            {
                using (SPWeb oweb = osite.OpenWeb())
                {
                    try
                    {
                        SPList olist = oweb.Lists.TryGetList("Training");
                        SPListItem oitem = olist.Items.Add();
                        oitem["Title"] = "listdata";
                        oitem["Sex"] = "Anand";
                        oitem["Age"] = "22";
                        oitem["Qualification"] = "BE";
                        oweb.AllowUnsafeUpdates = true;
                        oitem.Update();
                        olist.Update();
                        oweb.AllowUnsafeUpdates = false;
                        SPQuery oqry = new SPQuery();
                        oqry.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>Kumar</Value></Eq></Where>";

                        grd.DataSource = olist.GetItems(oqry).GetDataTable();
                        grd.DataBind();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }