Friday, 27 July 2012

The request failed with HTTP status 417: Expectation failed when SharePoint calling .asmx web service

I tried to access a web method in a .asmx web service from SharePoint web service.
To add a web reference steps
  1. Add Service Reference
  2. Click Advance button in the window
  3. Click Add Web Reference in the next window
  4. Then paste the asmx path add give a reference name then click ok
After this I tried to call the web method but it gives the  The request failed with HTTP status 417 error  I tried with Google finally I found a link in Code Project
http://www.codeproject.com/Articles/94235/The-request-failed-with-HTTP-status-417-Expectatio

Steps to Resolve
  • We need to have this line of code before making any web requests:  
         System.Net.ServicePointManager.Expect100Continue = false
 
  • Add these lines to the application's configuration file (between <configuration> and </configuration>): 
     
    <system.net>
     <settings>
      <servicePointManager expect100Continue="false" />
     </settings>
    </system.net>
     
     
    Now Everything goes fine.
     
    Happy Coding :)  

Saturday, 21 July 2012

Send mail using GMail SMTP

  

try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {

                    bool isAllowed = SPContext.Current.Web.AllowUnsafeUpdates;
                    SPContext.Current.Web.AllowUnsafeUpdates = true;
                    SmtpClient smtp;
                    MailMessage mail = new MailMessage();
                    string FromEmail = "s.sugunthan.mca@gmail.com";
                    string EmailPwd = "password";
                    smtp = new SmtpClient("smtp.gmail.com");
                    smtp.UseDefaultCredentials = true;
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.Credentials = new System.Net.NetworkCredential(FromEmail, EmailPwd);
                    smtp.EnableSsl = true;
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                    mail.Body = "Sample Content";
                    mail.To.Add(new MailAddress("sugunthan@gmail.com"));
                    mail.From = new MailAddress(FromEmail);
                    //Bybass the certifiate check
                    ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
                    smtp.Send(mail);
                    SPContext.Current.Web.AllowUnsafeUpdates = isAllowed;
                    ClearFields();
                    Clear_ErrorMsg();
                    lblResult.ForeColor = Color.Green;
                    lblResult.Text = "Mail Send Successfully!!";





                });
            }
            catch (Exception)
            {
                lblResult.ForeColor = Color.Red;
                lblResult.Text = "Sorry! Please Try Again!!";
            }

Email Validation in the C# code behind


  string pattern=@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
                System.Text.RegularExpressions.Match match =
                     Regex.Match(txtEmail.Text.Trim(), pattern, RegexOptions.IgnoreCase);

                if (match.Success)
                {
                }
                else
                {
                }

Wednesday, 11 July 2012

Error in using list view control in SharePoint 2010 visual webpart

Hi Tried to add a list view control in the SharePoint 2010 visual webpart but it give the error like following.


Line 8:  <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="BlogListUserControl.ascx.cs" Inherits="BlackintonWebparts.BlogList.BlogListUserControl" %>
Line 9: 
Line 10: <asp:ListView ID="ListViewProducts" runat="server" ItemPlaceholderID="ProductItem">
Line 11:     <ItemTemplate>
Line 12:

After that I found the namespace is not register so I register the name space

Add the following code under the <controls> section under <system.web> and pages in the webcofig file
 
<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />



Or add in the ascx file or aspx file



<%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI.WebControls" TagPrefix="asp" %>


Happy coding :))

Tuesday, 10 July 2012

SharePoint Interview Questions and Answers

1. What is SharePoint?
SharePoint is a platform for building collaboration solutions.
It supports
  • Document Management
  • Colloboration
  • Sharing and publishing information
  • Enterprise Search
  • Business Intelligence like Dashboard and Survey
  • Business Forms i.e Electronic forms and workflows
2. Site and Site Collection
  • Site is the collection of webpages and information in a organized way
  • Site having its own list and libraries likes images and documents
  • Site having the information about its users and their rights
  • It is very useful to fix the boundries
    Site collection is the collection of sites. It is the logical group of SharePoint site it having a top level site that includes the users and their information of the site collection.

3. Web application 
           Web application is the collection of site collection.  Simply web application is an iis virtual directory.  It having own application pool.

4.Custom Action 
            SharePoint ribbon has many icons to access.  The SharePoint developer can add his own menu icon in the ribbon and add the action also this is called custom action.  In SharePoint 2007 we can add our own menu item in the site actions menu and in the default display form.

5.WebPart, WebPart Zones, WebPart Pages
            WebPart is a widget it can be attachable to the webpage in the run time by the users.  It can be minimize, remove and configure its properties.
           WebPart Zone is the container to add the WebPart it is resizeable at the time of designing . We can add n number of WebParts inside the WebPart Zone.
           WebPart Page is like a normal webpage but it includes at-least one WebPart Zone.

6.Feature    
          Feature is a functional component it includes SharePoint instances like webpart, masterpage, custom action, list, etc and its own files like elements. Feature can activate and deactivatable.  When we activate the feature the sharepoint instances in the feature added to the feature scope.  The feature having its own event receivers the are handles the following events installing, uninstalling, activated and deactivated.  The scopes of the feature are 1.web 2.site 3. farm.

7.WSP files
           Windows Solution Package.  WSP file is the combination of the solution files and its dlls it includes the maifest file also.  It is like CAB file.
  
8. List Vs Document Library
              Almost both are same but the difference is list item can able to attach more than one files.  But document library can able to attach only single file, because the information all are about that single document.

9.Data Connection Library
               Microsoft Office SharePoint Server (MOSS) 2007 provides a business intelligence (BI) feature called the Data Connection Library (DCL), which lets you centrally store and manage Microsoft Office external data connections. Office external data connections are the external data source connection files that Microsoft Excel and InfoPath use for sourcing data from external data sources. Office external data connections are useful because you can reuse them—the end user doesn't need to remember the data source specifics (e.g., address, authentication). 







Custom Editor Part in SharePoint 2010 Webpart

WebPart is an excellent feature in SharePoint.  The webpart custom properties facilitates the webpart to extends its range in multiple websites.  But that webpart editor have lack of feature like it doesn't have any fields like textarea, password,validation and event handler also is not available.  It is very limited. 

Here is a possible to extend its function by using custom editor.  We develop our own editor for our webpart.

Steps 1.  Create Webpart properties but that is not display to the user so put browsable feature to false like the following


        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        public string Content
        {
            get { return _Content; }
            set { _Content = value; }
        }

        private string _LinkUrl;
        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        public string LinkUrl
        {
            get { return _LinkUrl; }
            set { _LinkUrl = value; }
        }


Step 2 : Override the CreateEditorParts Function and WebBrowsableObject



public override EditorPartCollection CreateEditorParts()
        {
            //Add custom CheckBoxList editor for editing NewsSources Property
            List<EditorPart> editors = new List<EditorPart>();
            editors.Add(
               new CustomEditor { ID = "Content Editor" });
            return new EditorPartCollection(editors);
        }

      
      public override object WebBrowsableObject
      {
          get { return this; }
      }

Note:  Here CustomEditor  is the custom class for the custom editor we must add in the same name space after that the webpart file is look like this.

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



namespace BlackintonWebparts.MiddleBox
{
    [ToolboxItemAttribute(false)]
    public class MiddleBox : Microsoft.SharePoint.WebPartPages.WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/BlackintonWebparts/MiddleBox/MiddleBoxUserControl.ascx";


        private string _Content;

        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        public string Content
        {
            get { return _Content; }
            set { _Content = value; }
        }

        private string _LinkUrl;
        [WebBrowsable(false)]
        [Personalizable(PersonalizationScope.Shared)]
        public string LinkUrl
        {
            get { return _LinkUrl; }
            set { _LinkUrl = value; }
        }


        protected override void CreateChildControls()
        {
            Control control = Page.LoadControl(_ascxPath);
            Controls.Add(control);
        }

        #region IWebEditable Members

        //create an instance of each custom EditorPart control
        //associated with a server control and return them as collection
        public override EditorPartCollection CreateEditorParts()
        {
            //Add custom CheckBoxList editor for editing NewsSources Property
            List<EditorPart> editors = new List<EditorPart>();
            editors.Add(
               new CustomEditor { ID = "Content Editor" });
            return new EditorPartCollection(editors);
        }

        //a reference to the associated server control
      public override object WebBrowsableObject
      {
          get { return this; }
      }

        #endregion

    }
}


The CustomEditor class look like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Collections;

namespace BlackintonWebparts.MiddleBox
{
    public class CustomEditor:EditorPart
    {

        private TextBox txtContent;
        private TextBox txtLinkUrl;
       

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            txtContent = new TextBox();
            txtContent.TextMode = TextBoxMode.MultiLine;
            txtLinkUrl = new TextBox();
        }

       
  
        protected override void  CreateChildControls()
        {
            base.CreateChildControls();
            this.Controls.Add(txtContent);
            this.Controls.Add(txtLinkUrl);
        }

        public override bool ApplyChanges()
        {

            //call to make sure the check box list is set up
            EnsureChildControls();
            var wb = WebPartToEdit as MiddleBox;
            if (wb == null) return false;
            wb.LinkUrl = txtLinkUrl.Text;
            wb.Content = txtContent.Text;
            return true;


        }

        public override void  SyncChanges()
        {


            //call to make sure the check box list is set up
            EnsureChildControls();   
           
            // get a reference to the corresponfing web part
            var wb = WebPartToEdit as MiddleBox;
            if (wb == null) return;

            txtLinkUrl.Text = wb.LinkUrl;
            txtContent.Text = wb.Content;


        }

    }

}



The Method Apply Changes Set the properties to the webpart and the method SynChanges retrive the data from the webpart properties.

Happy Coding :)