Friday, December 9, 2011

Creating a Folder in a SharePoint List and adding Items to it programatically

Below is the code snippet to create a folder in a SharePoint List and adding Items to it.
In the below snippet "lsit" is SPList object in which you are trying to create the folder.


// create a folder under the path specified 
web.AllowUnsafeUpdates =True;
folderItem = list.Items.Add(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder);

// set the folder name and update
folderItem["Title"] = "My Folder"
folderItem.Update();

//create a listitem object to add item in the foler
SPListItem listItem = list.Items.Add(folderItem.Folder.ServerRelativeUrl, SPFileSystemObjectType.File, null);

//Set the values for other fields in the list
listItem["Contact ID"] = <<Contact ID>>;
listItem["Contact Name"] = <<Contact Name>>;
listItem.Update();

web.AllowUnsafeUpdates =False;
web.update(); 


Hope this helps..

Add a new list item inside a folder using Sharepoint Webservice Lists.asmx


By using the following batch element you can create an item inside a folder present in a list or library.

<Batch OnError="Continue" RootFolder="http://moss/Lists/myList/FolderName">
  <Method ID="1" Cmd="New">
    <Field Name="ID">New</Field>
    <Field Name="CustomerName">Robert Hay</Field>
    <Field Name="CustomerAddress">Redmond</Field>
    <Field Name="Title">customer2</Field>
  </Method>
</Batch>
 
The important attribute here is the RootFolder. You need to provide the 
URL of the folder where you wish to create the item. So right now the 
RootFolder value is http://moss/Lists/myList/FolderName.
  
 
Hope this helps ..

Tuesday, September 27, 2011

How to programmatically send an email in SharePoint using C#


In this post I will discuss about Sending an e-mail in SharePoint 2010 using C#

public static bool SendMail(string Subject, string Body, bool IsBodyHtml, string From, string To, string Cc, string Bcc)
        {
            bool mailSent = false;
            try
            {
                SmtpClient smtpClient = new SmtpClient();
                smtpClient.Host = SPContext.Current.Site.WebApplication.
                OutboundMailServiceInstance.Server.Address;
                MailMessage mailMessage = new MailMessage(From, To, Subject, Body);
                if (!String.IsNullOrEmpty(Cc))
                {
                    MailAddress CCAddress = new MailAddress(Cc);
                    mailMessage.CC.Add(CCAddress);
                }
                if (!String.IsNullOrEmpty(Bcc))
                {
                    MailAddress BCCAddress = new MailAddress(Bcc);
                    mailMessage.Bcc.Add(BCCAddress);
                }
                mailMessage.IsBodyHtml = IsBodyHtml;
                smtpClient.Send(mailMessage);
                mailSent = true;
            }
            catch (Exception) { return mailSent; }
            return mailSent;
        }

Hope this helps..
Next Post- Programatically set the welcome page in Sharepoint

Popular Posts