Apr 2, 2010

How to access the Sharepoint control from Silverlight module through javascript

In the Previous post we accessed the public method inside the SIlverlight module from an .aspx page ,In this we are doing vice versa.
As per our objective we have a control inside a master page of a sharepoint site and a Silverlight control is hosted inside the webpart of a sharepoint page.So we need to access the sharepoint control when we click any button inside the Silverlight module through javascript.
So here are objective is to first access the control and then append a span tag to display some text inside the control on click of some button inside Silverlight control.
Followed Approach:
·          Go to the Page load event and include the below code snippet
--------------------------------------------------------------------------------------

HtmlPage.RegisterScriptableObject("page", this);
           
            HtmlDocument doc = HtmlPage.Document;
            strControlInnerHTMLGlobal = doc.GetElementById("SharepointControlID").GetProperty("innerHTML");

           
            strControlInnerHTML = strControlInnerHTMLGlobal + "<span >&gt;</span><span >Added in Sharepoint control through Silverlight button click </span>";
            doc.GetElementById("SharepointControlID").SetProperty("innerHTML", strControlInnerHTML);
           
--------------------------------------------------------------------------------------
In above code snippet "SharepointControlID” is the ClientId of sharepoint control.
 We are taking the object of control through its ClientId and then getting the innerHTML of the control which is in the form ‘span’ tags, and then we are appending the required span tag in the control.
Note: Here we are using GetProperty and SetProperty method through which we get the innerHTML of the control and then we can change the value as per our requirement.
Happy Coding J

How to access the silverlight method through javascript

Fisrt time I am writing anything about Silverlight part .
Here scenario is that we need to access a control inside silverlight application and then need to execute some operation on that control through javascript which is written outside the silverlight application ,now consider it inside the hosting .aspx page.
Since directly we cannot access the silverlight control in the javascript as on rendering of page ,silverlight control does not come in the viewsource of the page. So the approach to expose the control is to access the public method inside silverlight application and then perform the required operation.
To achieve this functionality we are following the below approach
  • In the Loaded event or the constructor of the page we have to make call to the ( RegisterScriptableObject) method. It accepts two parameters:
    • A name, which  is used to  make calls to the application.
    • An object, which will be called.
Code snippet which is required to be added in the loaded event of the

HtmlPage.RegisterScriptableObject("page", this);

Note: Here ‘this’ is the page object.
We can call only public methods in Silverlight application from javascript . If we  want a method to be accessed from JavaScript, you have to add the ‘ScriptableMember’ attribute before the method declaration. Code Snippet which is to be added inside the xaml.cs of the control :

    [ScriptableMember]
        public void Start()
        {
// Do Something
        }


Now to access this public method from javascript .write this javascript method inside the .aspx page or the place from where you want to call the javascript.

<script type="text/javascript" language="javascript">
       function CallSilverLight() {
           var slControl = document.getElementById("_slControl");
           slControl.Content.page.Start();
         
       }
   </script>

1)       Here id is the control ID of Silverlight control .
2)       In this statement slControl.Content.page.Start();
‘page’ is the name which we have given while registering the Scriptable control in the loaded event of the page
HtmlPage.RegisterScriptableObject("page", this);

3)       ‘Start’ is the name of the public method which is used to select the tab on click of breadcrumb menu which is inside sharepoint
And finally call this javascript from any control or  on page load as per the requirement.

Initially this seems to be bit tricky ,but this is very usefull approach for interaction between Silverlight and .aspx page.
Please write to me if you face any problem while implementing it or if there is any other better approach.

Happy Coding J

Modifying Web.config programmatically using SPWebConfigModification class

Modifying Web.config programmatically

Often we need to add entries in web.config either for Webparts , Http Module ,Connection string or for any other module.For this either we need to add entries manually or we can automate this process using code.We can provide the user an option of On/Off for adding/removing the entries from the web.config . This can be done using feature and associating the feature receiver which will add the entries on Feature Activated and removes the entries on Deactivation of feature.
For this we will use the “SPWebConfigModification” class which will access the web.config of the web application through object model.
Objective: We will insert the HttpModule entries in the Web.config under the ‘HttpModule’ section
Steps are follows as:
Create a feature With scope as WebApplication/Site/Web. Here we will create the feature with scope as WebApplication.
In the Feature.xml
<?xml version="1.0" encoding="utf-8" ?>
<Feature Id="{}"
         Title="HTTP Module Configuration"
         Description="This feature modifies the Web Application web.config to insert the HTTP Module entries."
         Scope="WebApplication"
         ReceiverAssembly="’AssemblyName’, Version=1.0.0.0, Culture=Neutral, PublicKeyToken= "
         ReceiverClass="’ClassName’"
         xmlns="http://schemas.microsoft.com/sharepoint/">
               
</Feature>

Note: Add the feature Receiver class reference in the feature.xml
FeatureReceiver Class code: FeatureActivated method
In this method we will add the entries in the Web.config
public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            var oWebApp = properties.Feature.Parent as SPWebApplication;

            try
            {
                if (!oWebApp.IsAdministrationWebApplication)
                {
                    ModifyWebConfigEntries(oWebApp);
                }
            }
            catch (Exception ex)
            {

            }
        }

‘ModifyWebConfigEntries’ Method Code :
public void ModifyWebConfigEntries(SPWebApplication oWebApp)
        {
            #region Http Module Section

            webConfigModifications = new SPWebConfigModification();
            webConfigModifications.Path = "configuration/system.web/httpModules";
            webConfigModifications.Name = "add[@name='CustomHttpModule']";
            webConfigModifications.Sequence = 0;
            webConfigModifications.Owner = "addCustomModule";

            webConfigModifications.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            webConfigModifications.Value = @"<add name='CustomHttpModule' type='ClassName,AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cfba5cfbc4661d2d' />";
            oWebApp.WebConfigModifications.Add(webConfigModifications);

            #endregion

            SPWebService.ContentService.WebApplications[oWebApp.Id].Update();
            //Applies the web config settings in all the web application in the farm
            SPWebService.ContentService.WebApplications[oWebApp.Id].WebService.ApplyWebConfigModifications();
        }


 Note : This ApplyWebConfigModifications() method applies changes in all the web.config which are under the same farm.
Few things to note :
Path : This is the node under which we need to add the entry
Name : Name property is very useful because while retrieving from the database this entry should be in correct format.
Owner : This property is used while deleting the entries from the web.config programmatically

Now we have added the entries in the web.config programmatically.we need to remove these entries also when the feature is no longer in use.
While removing the entries we will take all the entries based on the owner property and delete the required entries.

var oWebApp = properties.Feature.Parent as SPWebApplication;

            try
            {

                if (!oWebApp.IsAdministrationWebApplication)
                {
                    // Web application is not central administration.  Configuration changes are different.
                    DeleteWebConfigEntries(oWebApp);
                }
            }
            catch (Exception ex)
            {
            }

‘DeleteWebConfigEntries’ Method :

public void DeleteWebConfigEntries(SPWebApplication oWebApp)
        {
            Collection<SPWebConfigModification> oCollection = oWebApp.WebConfigModifications;
            int iStartCount = oCollection.Count;
            for (int c = iStartCount - 1; c >= 0; c--)
            {
                SPWebConfigModification oModification = oCollection[c];

                if (oModification.Owner == "addCustomModule")

                    oCollection.Remove(oModification);
            }

            if (iStartCount > oCollection.Count)
            {
                oWebApp.Update();
                //Applies the web config settings in all the web application in the farm
                SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
            }
        }

Note :Here We are deleting the entries based on the owner property of the Modified entry.
First we take the collection of all the modifications in the Web.config through WebConfigModification method and then remove these entries.
So your job is done

Important thing to remember is that if we are adding the entries in the web.config programmatically then we should delete those entries progrmmatically only because while adding entries throug object model ,entries goes into the content DB of the site and on removing those entries manually this can cause conflict in the  version which exist in the file structure and the version in the content DB.

Here is very good article from MSDN which explain this class in detail.

Happy coding J