Monday, 9 April 2012

How to create and send outlook apointment in c# without any warning from Outlook

I received a requirement that send outlook appointment to the attendees of the meeting automatically after they booked the meeting.    
For that I found a solution to send a outlook appointment to the attendees.

Please add reference 'Microsoft.Office.Interop.Outlook'  Verson 11


private void test(string name, string location, string startTime, string endTime, string body, string email)
{
  Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application(); // creates new outlook app
            Microsoft.Office.Interop.Outlook.AppointmentItem oAppointment = (Microsoft.Office.Interop.Outlook.AppointmentItem)outlookApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem); // creates a new
            oAppointment.Subject = "Meeting""; // set the subject
            oAppointment.Body = "This is important"; // set the body
            oAppointment.Location = "NewYork"; // set the location
            oAppointment.Start = Convert.ToDateTime(startTime); // Set the start date
            oAppointment.End = Convert.ToDateTime(endTime); // End date
            oAppointment.ReminderSet = true; // Set the reminder
            oAppointment.ReminderMinutesBeforeStart = 15; // reminder time
            oAppointment.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh; // appointment importance
            oAppointment.BusyStatus = Microsoft.Office.Interop.Outlook.OlBusyStatus.olBusy;
            oAppointment.Save();
            Microsoft.Office.Interop.Outlook.MailItem mailItem = oAppointment.ForwardAsVcal();
            // email address to send to
            mailItem.To = email;
            // send
            mailItem.Send();
        }

Now the appointment was sending successfully.....

Important note you must configure you outlook in your machine because the from address is automatically fetched from the outlook account.

But I received a warning message like 'A program is trying to send an e-mail message on your behalf. If this is unexpected, click
deny and verify you antivirus software is up-to-date.'

This is terrible because the administrator of the server is not responsible to click allow or deny each and every time.

To stop this please follow the steps;
In outlook 2007 
1. Go to Toools->Trust Center
2. Select 'Programatic Access'
3. There select 'Never warn me about suspicious access'  because my antivirus was expired...... so check your antivirus and select the first option i.e 'Warn me about suspicious access when my antivirus is in-active or out-of-date'

Happy Coding :) 

Saturday, 7 April 2012

How to import user profile from Active directory to Shared Service Provider in SharePonit 2007

I created the users in the Active Directory in Windows Server 2003 then added users to the SharePoint site.  But I cant get the information which all are given in the Active Directory user profile.

I found a way to solve this the steps are given....
1. Open SharePoint Central Administrator
2. Open Shared Service administrator(link in the left panel).
3. Click "User Profile and Properties" under "User Profile and MySite".
4. There you can see a link named "Start Full Import".
5. Wait until all profiles imported.
6. Restart IIS
7. Go to Services and restart the "Windows SharePoint Services Timer"
8. Now you get all information in the site and Visual Studio also.

Happy Coding :)




Friday, 6 April 2012

Add Person/Group field to a SharePoint list from Webpart

I created a new list with person/group field as follows
Then create a WebPart with checkbox list which displays the users and button to add user to the list.
The following code explains how to do this.....

formula to add a user
<UserID>;#<UserLoginName>


using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace userselect
{
    [Guid("cdcee41f-cb36-42eb-b2d8-36af57fbcbd8")]
    public class userselect : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private bool _error = false;
        private string _myProperty = null;
        Dictionary<int,string> UserInfo= new Dictionary<int,string>();
        Button AddUser;
        TextBox txtInfo;
        CheckBoxList UserList;
        public userselect()
        {
            this.ExportMode = WebPartExportMode.All;
        }
   protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {

                    base.CreateChildControls();
                    UserList = new CheckBoxList();
                    Get_UserList();
                    UserList.ID = "UserList";
                    UserList.DataSource =UserInfo;
                    UserList.DataValueField = "Key";
                    UserList.DataTextField = "Value";
                    UserList.DataBind();
                    Controls.Add(UserList);

                    AddUser = new Button();
                    AddUser.ID = "AddUser";
                    AddUser.Text = "Add User";
                    AddUser.Click += new EventHandler(AddUser_Click);
                    this.Controls.Add(AddUser);

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

        void AddUser_Click(object sender, EventArgs e)
        {
        string temp=string.Empty;
        SPSite site = new SPSite("http://url");
        SPWeb web = site.OpenWeb();

            for (int index = 0; index < UserList.Items.Count; index++)
            {
                if (UserList.Items[index].Selected)
                {
                int id=int.Parse(UserList.Items[index].Value);
                temp = id.ToString()+";#"+web.Users.GetByID(id).LoginName+";#";
               }
            }
            txtInfo.Text = temp;
//remove the extra symbol
            if (temp.EndsWith(";#"))
            {
                temp = temp.Substring(0, temp.Length - 2);
            }

            SPList list = web.Lists["newlist"];
            SPListItem item = list.Items.Add();

            item["Title"] = "from sugu";
            item["user"] = temp;
            item.Update();


        }

        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls();

                    // Your code here...
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }


        private void Get_UserList()
        {
            UserInfo.Clear();
           
            SPSite site = new SPSite("http://url");
            SPWeb web = site.OpenWeb();
           // I need the users only from the viewers group
            SPGroup group = web.Groups["Viewers"];
           
            foreach (SPUser user in group.Users)
            {
                UserInfo.Add(user.ID, user.Name);
               
            }
          
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
        }
    }
}


Happy coding :)




Friday, 30 March 2012

How to add style sheet for the shrepoint 2007 webpart

It is some bit confuson to provide css style sheet to the sharepoint webpart.  Because where we have place it in out project and is it acceptable by the destination point server.
First we look up the 12 hive in out sharepoint server. The stylesheets are available in the folder C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\STYLES.

yes this is the right place to keep our webpart's style sheet.  How we are going to achieve this.  No need to sweat these things are carried by wsp builder very neatly.. 

If you create a new sharepoint webpart using wspbuilder you can able to see the folder structure with many folder there you can see a foler named 12 that is really point out the 12 hive.

so our remaining work is create the folder structure as like the above mentioned  12\template\layout\styles\styles\<your webpart name>\

Then create the style sheet and refer it in your webpart.

When you are going to install your webpart in the client side it automatically added to the 12 hive and removed if you uninstall it.

Happy Coding:)
SSS.



How to Add Custom Webpart Properties in SharePont 2007

Webpart is a nice feature in sharepoint.  Most of the time we need to get some details from the user before the webpart started to work such as the which list you want to proceed.

We can see some default properties are available in the proprety page like appearance and advanced.

The following code explain you how to create a webart with custom property.

 
First off, create a new class that inherits from Microsoft.SharePoint.WebPartPages.ToolPart.
public class SampleToolPart : ToolPart
{
    // First, override the CreateChildControls method. This is where we create the controls.
    protected override void CreateChildControls()
    {
        // create a panel that will hold all of our controls
        Panel toolPartPanel = new Panel();
        toolPartPanel.CssClass = "ms-ToolPartSpacing";
        // create a table that will put our controls in rows
        Table toolPartTable = new Table();
        toolPartPanel.Controls.Add(toolPartTable);
        toolPartTable.CellPadding = 0;
        toolPartTable.CellSpacing = 0;
        toolPartTable.Style["border-collapse"] = "collapse";
        toolPartTable.Attributes.Add("width", "100%");
        // create the first row that will contain a control
        TableRow firstRow = new TableRow();
        toolPartTable.Rows.Add(firstRow);
        TableCell firstRowCell = new TableCell();
        firstRow.Cells.Add(firstRowCell);
        // add formatting to the cells
        firstRowCell.Controls.Add(new LiteralControl("<div class='UserSectionHead'>Sample Dropdown:</div>"));
        firstRowCell.Controls.Add(new LiteralControl("<div class='UserSectionBody'><div class='UserControlGroup'><nobr>"));
        // create the actual control
        DropDownList sampleDropDown1 = new DropDownList();
        sampleDropDown1.ID = "sampleDropDown1";
        sampleDropDown1.Items.Add("Item 1");
        sampleDropDown1.Items.Add("Item 2");
        sampleDropDown1.Items.Add("Item 3");
        // add the new control to the table
        firstRowCell.Controls.Add(sampleDropDown1);
        // add formatting closing tags to the cell
        firstRowCell.Controls.Add(new LiteralControl("</nobr></div></div>"));
        // create a standard dotted line seperator
        TableRow seperatorRow = new TableRow();
        toolPartTable.Rows.Add(seperatorRow);
        TableCell seperatorCell = new TableCell();
        firstRow.Cells.Add(seperatorCell);
        seperatorCell.Controls.Add(new LiteralControl("<div style='width:100%' class='UserDottedLine' />"));
        // lather, rinse, repeat for all the controls you need
        // finally add the panel to the controls collection of the tool part
        Controls.Add(toolPartPanel);
        base.CreateChildControls();
    }
    // Next, override the ApplyChanges method.
    // This method is where we will persist the values that the user selects.
    public override void ApplyChanges()
    {
        // get the parent webpart
        SampleWebPart parentWebPart = (SampleWebPart)this.ParentToolPane.SelectedWebPart;
        // loop thru this control's controls until we find the ones that we need to persist.
        RetrievePropertyValues(this.Controls, parentWebPart);
    }
    // Recursive function that tries to locate the values set in the toolpart
    private void RetrievePropertyValues(ControlCollection controls, SampleWebPart parentWebPart)
    {
        foreach (Control ctl in controls)
        {
            RetrievePropertyValue(ctl, parentWebPart);
            if (ctl.HasControls())
            {
                RetrievePropertyValues(ctl.Controls, parentWebPart);
            }
        }
    }
    // Method for retrieving the values set by the user.
    private void RetrievePropertyValue(Control ctl, SampleWebPart parentWebPart)
    {
        if (ctl is DropDownList)
        {
            if (ctl.ID.Equals("sampleDropDown1"))
            {
                DropDownList drp = (DropDownList)ctl;
                if (drp.SelectedItem.Value != "")
                {
                    parentWebPart.Property1 = drp.SelectedItem.Value;
                }
            }
        }
    }
}

Now, we need to wire up the tool part to the web part. To do this, we override the GetToolParts() method and place our new tool part in the ToolPart array.
 
public class SampleWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
    public SampleWebPart()
    {
        this.ExportMode = WebPartExportMode.All;
    }
    // property that will be set thru our custom tool part
    // make sure to set the WebBrowsable attribute to false
    // so it will not show up outside of our custom tool part
    [Personalizable(PersonalizationScope.Shared)]
    [WebBrowsable(false)]
    [System.ComponentModel.Category("My Group")]
    [WebDisplayName("Property1")]
    public string Property1
    {
        get
        {
            return _property1;
        }
        set
        {
            _property1 = value;
        }
    }
    private string _property1 = "Default Value";
    public override ToolPart[] GetToolParts()
    {
        // resize the tool part array
        ToolPart[] toolparts = new ToolPart[3];
        // instantiate the standard SharePopint tool part
        WebPartToolPart wptp = new WebPartToolPart();
        // instantiate the custom property toolpart if needed.
        // this object is what renders our regular properties.
        CustomPropertyToolPart custom = new CustomPropertyToolPart();
        // instantiate and add our tool part to the array.
        // tool parts will render in the order they are added to this array.
        toolparts[0] = new SampleToolPart();
        toolparts[1] = custom;
        toolparts[2] = wptp;
        return toolparts;
    }
}

Happy Coding :)

Friday, 23 March 2012

VSeWSS Service Error

I tried to create a new webpart using Visual Studio 2008 with VSeWSS3.0 but I cant deploy my webpart successfully.
I found error like the following

VSeWSS Service Error: Error loading assembly (dll location)

VSeWSS Service Logging Error: Access to the path 'C:\Documents and Settings\Default User\Application Data\Microsoft\VSeWSS 1.3' is denied.

Logging failed attempting to write to C:\Documents and Settings\Default User\Application Data\Microsoft\VSeWSS 1.3\VSeWSS1.3 service.log. This may occur because the VSeWSS WCF Service does not have local administrator permissions. Please review the release notes.        




I tried to change the property of the particular folder(C:\Document and Settings\...) to uncheck the read only.
But I cant uncheck it.

Find I found the error is really resides in the application pool.

I followed the steps

1.  Go the INet Manager
2.  Go the Application Pool Where VSeWSS Service resided and right click that open the property page.
3.  Change the UserName and Password.

Now the Visual Studio Working Fine......

Happy Coding :)

Thursday, 22 March 2012

Feature in SharePoint

         Feature is XML file which is used to activate or deactivate a facility in the SharePoint.  Feature includes the element manifest file which explains the elements which all are included in this feature.     
          Feature have different Scope levels 1. Site level  2. Farm level   3.Web Level
          Feature can activate some other features this is called feature stabling.