Monday, February 27, 2012

SQL Server : Sample for reading xml data

Goal

Simple SQL query to fetch data from an xml.
--//---------- Declare ----------//
DECLARE @xmlSample XML;

SET @xmlSample = '<GUIDs>
  <GUID Temp="a">1</GUID>
  <GUID Temp="a">2</GUID>
  <GUID Temp="a">3</GUID>
  <GUID Temp="b">4</GUID>
  <GUID Temp="b">5</GUID>
</GUIDs>';

--//---------- Set Query ----------//
SELECT * FROM 
(
    SELECT 
        d.value('.', 'INT') AS GUID , 
        d.value('@Temp', 'VARCHAR(36)') AS Temp
    FROM @xmlSample.nodes('/GUIDs/GUID') d(d) 
) AS A


Wednesday, February 22, 2012

SharePoint 2010 : Login the system as Current Windows Authenticated user without code.

Problem

Everytime I come on my SharePoint Server and open a site, I have to add my credentials to get in the site. What should be done to login default as a current user logged in Windows.

Solution

  1. Start Internet Explorer
  2. Security Tab
  3. Local Intranet
  4. Click "Sites"
  5. Click "Advanced"
  6. Add the local domain
  7. Save everything.
  8. Restart the browser


This should take care of the issue.

Important Note

Mostly this is not considered as a good option, but being a developer who wants to waste time in entering the login credentials again and again. :P

SharePoint 2010 : Using ECMA script recall javascript function on control click in UpdatePanel.

I dont know if this is important or not but I found this thing interesting to blog.

Goal

There is a in UpdatePanel on the page. We have to call a javascript everytime the page is postback. Even if it is a postback from a control under UpdatePanel. I discovered a way using ECMA using which we can easily call the javascript function.

Solution

<script>!window.jQuery && document.write('<script src="http://code.jquery.com/jquery-1.4.2.min.js"><\/script>');</script>
<script type="text/javascript">
    //Call the function or assign the event on ready
    $(document).ready(function(){
        //This will wait until script is loaded and than call our function
        ExecuteOrDelayUntilScriptLoaded(PageLoadEvent, "sp.js");
    });
    
    //This function is the most important function that will add the end request
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(FunctionHandler);
    
    //Event handler that fires on the UpdatePanel control events too
    function FunctionHandler(sender, args) {
        ExecuteOrDelayUntilScriptLoaded(ButtonClickEvent, "sp.js");
    }
    
    //Write the time on page load
    function PageLoadEvent(){
        alert('Page Load : ' + (new Date()).toString() );
    }

    //Will call on button click event
    function ButtonClickEvent(){
        alert('Button Click : ' + (new Date()).toString() );
    }                
</script>
<asp:UpdatePanel ID="upPanel" runat="server">
    <ContentTemplate>
        <asp:Button ID="btn" runat="server" Text="Click here!!!" />
    </ContentTemplate>
</asp:UpdatePanel>


Important Note

The most important thing in this is, if you remove "Sys.WebForms.PageRequestManager.getInstance().add_endRequest(FunctionHandler);" and try clicking the button the event wont be raised to call "function ButtonClickEvent()". So that is the magic trick.

Let me know if this helped you!

Thursday, January 19, 2012

SharePoint 2010 - You must have manage user profiles administrator rights to use administrator mode.

Goal

As the error metions the user should be "Admin in User Profile Service". The error comes when a user try to update a profile(other user's profile). Please read my last blog for more clarification SharePoint 2010 - Update user's infomation using UserProfileManager programmatically.

Fix

  • SharePoint 2010 Central Administration
  • Application Management > Manage Service Applications
  • Select the "User Profile Service Application" tab. On the top you can find an option of "Administrators".
  • Apply the user you are using in the Administrators and give rights to the user. I am using "Administrator" as a user.
  • Let me know if this is helpfull.

SharePoint 2010 - Update user's infomation using UserProfileManager programmatically.

Goal

To update a user's information using C#. In the below given sample i am changing the Email(WorkEmail) of a user.

Important facts

  • User trying to update a profile should be updating own record or should be an "Admin in User Profile Service". If the user is not an Admin in User Profile Service the error will come "You must have manage user profiles administrator rights to use administrator mode.".
  • In below given code I am using true for IgnoreUserPrivacy. Default it should be false but i also wanted to make this work.
    UserProfileManager(SPServiceContext.Current , true)
  • Most Important : The user i am using to run this is an Admin in User Profile Service. So the code is working fine.

Code

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite _site = new SPSite(SPContext.Current.Site.Url))
    {
        using (SPWeb _web = _site.OpenWeb())
        {
            //Gets the profile manager object
            UserProfileManager _profileManager = new UserProfileManager(SPServiceContext.Current , true);
                           
            //UserName
            string _userName = @"MyMachine\User";

            //Gets the profile of a User
            UserProfile _userProfile = _profileManager.GetUserProfile(_userName);
            if (_userProfile != null)
            {
                //Change the Email value
                ((UserProfileValueCollection)_userProfile["WorkEmail"]).Value = "sample@someDomain.com";
                               
                //Save the record
                _userProfile.Commit();
                               
            }
        }
    }
});

This code works absolutely fine. For the developers who are getting issue like "You must have manage user profiles administrator rights to use administrator mode." please click the error to visit the solution.