Friday, July 20, 2012

SharePoint Feature Receiver : System.InvalidCastException: Unable to cast object of type 'Microsoft.SharePoint.SPSite' to type 'Microsoft.SharePoint.SPWeb


If you trying to activate masterpage in the site collection level and if you are using below code this will thrown error

this code is referring only website not site collection
  public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
           SPWeb currentWeb = (SPWeb)properties.Feature.Parent;
            currentWeb.MasterUrl = "/_catalogs/masterpage/CustomMaster.master";
            currentWeb.CustomMasterUrl = "/_catalogs/masterpage/CustomMaster.master";
            currentWeb.Update();
        }



Error: 
Feature receiver assembly 'TestMasterPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9c3395ff06cc524a', class 'TestMasterPages.Features.CustomMaster.CustomMasterEventReceiver', method 'FeatureActivated' for feature '0ab67b2b-2360-45f2-b26e-e33a49ceea3c' threw an exception: System.InvalidCastException: Unable to cast object of type 'Microsoft.SharePoint.SPSite' to type 'Microsoft.SharePoint.SPWeb'.   
 at TestMasterPages.Features.CustomMaster.CustomMasterEventReceiver.FeatureActivated(SPFeatureReceiverProperties properties)   
 at Microsoft.SharePoint.SPFeature.DoActivationCallout(Boolean fActivate, Boolean fForce)


Solution:




  public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
          SPSite currentSite = (SPSite)properties.Feature.Parent;
            SPWeb currentWeb = currentSite.RootWeb;
            currentWeb.MasterUrl = "/_catalogs/masterpage/CustomMaster.master";
            currentWeb.CustomMasterUrl = "/_catalogs/masterpage/CustomMaster.master";
            currentWeb.Update();
        }



Cannot convert type 'Microsoft.SharePoint.WebControls.DelegateControl' to 'System.Web.UI.IAttributeAccessor'


When you are working with master page using SharePoint designer sometimes junk data will be added to the master page as shown below. 

in my scenario junk data added to <SharePoint:DeveloperDashboard control

  <div id="DeveloperDashboard" class="ms-developerdashboard">
                     <SharePoint:DeveloperDashboard runat="server" id="ddashboard" __designer:Preview="[ DeveloperDashboard &quot;ddashboard&quot; ]" __designer:Values="&lt;P N='ID' T='ddashboard' /&gt;&lt;P N='Page' ID='1' /&gt;&lt;P N='TemplateControl' ID='2' /&gt;&lt;P N='AppRelativeTemplateSourceDirectory' R='-1' /&gt;"/>
              </div>


Error:


http://server/sites/sitecol1/_catalogs/masterpage/CustomMaster.master(502): error CS0030: Cannot convert type 'Microsoft.SharePoint.WebControls.DeveloperDashboard' to 'System.Web.UI.IAttributeAccessor'
        
Solution : 

 To fix this issue, you can search the whole master page has value like " __designer:Preview " or " "__designer:Values ", remove them using notepad file or SharePoint designer which ever is possible.


Tuesday, July 17, 2012

Call JavaScript in SharePoint



<
asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">

<script language="javascript" type="text/javascript">

  function CallJavascript()
    {
       alert("This is SharePoint");
    }

    _spBodyOnLoadFunctionNames.push(" CallJavascript ");

</script>

</ asp:Content>

Friday, July 6, 2012

SharePoint 2010 : Delete extended web application of any zone



You cannot see extended web application in the CA. you have to select default zone web application.

 you can delete extranet/intranet/internet extended web sites using below steps

Central Administration > Manage Web Applications > Select your  Web Application as shown in picture > select Delete from ribbon > Remove sharepoint from IIS website



Select IIS web site and zone to remove

Using Powershell

Remove-SPWebApplication -identity http://sitename -Zone {Default | Intranet | Internet | Internet | Custom | Extranet} -Confirm

his command removes the zone that you have specified for the Web application extension at http://sitename. This command does not remove the content databases or the IIS Web site.

 Note:
Be aware that this cmdlet can be used to delete the Web application, so be sure to use the appropriate parameters.

Thursday, July 5, 2012

Internet Explorer cannot display the webpage SharePoint 2010


Recently I came across a problem like
   Internet Explorer cannot display the webpage SharePoint 2010
When I create a web application in Sharepoint 2010. 

I checked with all scenarios
  1. ·         Application pool in running fine
  2. ·         Restarted iis server
  3. ·         Database server is fine
  4. ·         Checked Service account account

Everything is fine but not able access sharepoint site. At last I realized that with the port number that I was using. Actualy I am using  http://servername:110 port number. This one is reserved port no for pop3
Just make sure the port number which you are using before creating a web application.

 here are the list of port number.






Common port numbers

The Internet Assigned Numbers Authority (IANA) is responsible for the global coordination of the DNS Root, IP addressing, and other Internet protocol resources. This includes the registration of commonly used port numbers for well-known Internet services.
The port numbers are divided into three ranges: the well-known ports, the registered ports, and the dynamic or private ports. The well-known ports are those from 0 through 1023. Examples include:
The registered ports are those from 1024 through 49151. IANA maintains the official list.[2] The dynamic or private ports are those from 49152 through 65535. One common use is forephemeral ports.

List of TCP and UDP port numbers


Monday, July 2, 2012

SharePoint 2010 : Alternate Access Mappings Programmatically

here is the good post on this

http://www.directsharepoint.com/2012/01/get-current-web-application-alternate.html

or


            foreach (SPAlternateUrl altUrl in oWebApp.AlternateUrls)
            {
                if (altUrl.UrlZone == SPUrlZone.Default)
                {
                    incomingURL = SPUtility.AlternateServerUrlFromHttpRequestUrl(altUrl.Uri).AbsoluteUri;
                }
            }



SharePoint 2010 FBA : Add FBA users to SharePoint group programmatically


SharePoint stores FBA users to below format

i:0#.f|sqlmembershipprovider|Surya (please replace with your membership provider name)

 here i is identity provider
      f is membership provider name 



you may get below error when the format is wrong

The user does not exist or is not unique.


   void AddUserstoGroup()
        {
            string siteurl = “yoursiteurl”;

          
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(siteurl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        web.AllowUnsafeUpdates = true;
                        //string userName = "i:0#.f|sqlmembershipprovider|kumar";
                        //userName = userName.Split('|')[2];

                        string userName = "i:0#.f|sqlmembershipprovider|" + txtEmail.Text;
                        web.SiteUsers.Add(userName, txtEmail.Text,txtLastName.Text + "," + txtFirstName.Text, "");

                        SPUser user = web.SiteUsers[userName];
                        if (user != null)
                        {
                            //TNI Visitors
                            web.Groups["Visitors"].AddUser(user);
                            web.Update();
                            web.AllowUnsafeUpdates = false;
                        }

                        web.Update();
                        web.AllowUnsafeUpdates = false;

                    }
                }
            });



        }

Followers