SharePoint 2007 : My Calendar web part login problem for different accounts

 Introduction
When i work with
my calendar web part in SharePoint i got lot of questions and things happend. I
configured every thing correctly in My Calendar web part. i.e. Mail server
address, Mailbox.
But, when i open the page which contains calendar web part,
it prompts for user name and password of SharePoint site and another for the
Calendar login . i.e. login page of OWA application. If i provide the
credentials it will work very fine.

Now the problem starts. If the same
application is trying to access by another users, other than me then they always
failed to login to my calendar web part. This is because, in mailbox i given my
mail account, when others try to access the web part, it won't work at all. What
they need at that time is they will edit the web part, and will change the mail
box entry to their own email address. But this is not possible for all users
because all users don't have access to edit page/web part.

My Calendar
web part is not working for multiple users in SharePoint 2007. When you add the
web part on a page, which mail entry you given in the Mail box that account only
can login. I will tell you the reason behind it. My calender web part is
developed only for the purpose of showing their calendar events on the my site page. My site usually have access to
edit/delete etc for current logged in user, so he will see the content belongs
to him. But as per my requirements i want to use the calendar on the site other
than the my site pages.

Solution
To solve the above mentioned
problem, i created a custom web part to work for any logged in user. You can get
this code and build it, add the appsetting variables in the web.cofig and deploy
it to your site, You can get the deploy instructions of web part in
MSDN.

Web.Config changes:
We
need to provide 2 inputs to the web part.
1. Site Url.
2. Exchange Server
Url.

Requirements:
We have
setup a job [Usually we will setup while installing SharePoint and configuring
the SSP] in your SharePoint server Shared services Provider [SSP], to pull user
accounts from the Active Directory to Sharepoint profile database. Because below
code gets the emailid automatically from the user profile database depending on
the logged in user. And places that emailid in Mail Box entry of the my calendar
web part.

Usually, when we configure the Profile import, we will give
source and configure the schedule too. i.e. Every day night or every week etc…
it pulls data from given source [ex: Active directory] to profile
database.



 
  1. using System;
     

  2. using System.Collections.Generic;
     

  3. using System.Text;
     

  4. using Microsoft.SharePoint.Portal.WebControls;
     

  5. using Microsoft.SharePoint.Portal.Topology;
     

  6. using Microsoft.SharePoint.Portal;
     

  7. using Microsoft.SharePoint.Portal.UserProfiles;
     

  8. using System.Runtime.InteropServices;
     

  9. using System.Web.UI;
     

  10. using System.Web.UI.WebControls;
     

  11. using System.Web.UI.HtmlControls;
     

  12. using System.Configuration;namespace MyCalendarWebPart  

  13. {  

  14. [Guid("15241046-2128-4a1d-b6d5-24c8a67c4d28")]
     

  15. public class MyCalendarWebPart : Microsoft.SharePoint.WebPartPages.WebPart
     

  16. {  

  17. private OWACalendarPart wpCalendar;
     

  18. HtmlGenericControl litMsg = null;  

  19. HtmlGenericControl roundedCorner;  

  20.  

  21. protected override void CreateChildControls()  

  22. {  

  23. wpCalendar = new OWACalendarPart();  

  24. litMsg = new HtmlGenericControl();  

  25. roundedCorner = new HtmlGenericControl();  

  26. roundedCorner.InnerHtml = "";  

  27.  

  28. Controls.Add(configureCalendar());  

  29. Controls.Add(litMsg);  

  30. Controls.Add(roundedCorner);  

  31.  

  32. base.CreateChildControls();
     

  33. }  

  34.  

  35. private OWACalendarPart configureCalendar()
     

  36. {  

  37. try 

  38. {  

  39. //Connect to the portal and get the portal context.
     

  40. TopologyManager topology = new TopologyManager();  

  41. PortalSite portal = topology.PortalSites[new Uri(ConfigurationManager.AppSettings["SiteUrl"])];  

  42. PortalContext context = PortalApplication.GetContext(portal);
     

  43.  

  44. //initialize user profile config manager object
     

  45. UserProfileManager profileManager = new UserProfileManager(context);
     

  46. UserProfile profile = profileManager.GetUserProfile(true);  

  47.  

  48. wpCalendar.Title = "My Calendar";  

  49. wpCalendar.ViewName = "Weekly";  

  50. wpCalendar.CssClass = "";  

  51.  

  52. // use the profile object to retrieve the properties you need in your company to
     

  53. // retrieve the mail box name
     

  54. string workmail = profile[PropertyConstants.WorkEmail].ToString();
     

  55.  

  56. wpCalendar.MailboxName = workmail;  

  57. wpCalendar.OWAServerAddressRoot = ConfigurationManager.AppSettings["ExchangeServerUrl"];  

  58.  

  59. wpCalendar.Height = "655″;  

  60. wpCalendar.Width = "600″;  

  61. wpCalendar.ImportErrorMessage = "No EmailID found for your account.";  

  62. }  

  63. catch 

  64. {  

  65. litMsg.InnerHtml = "No EmailID found for your account.";  

  66. }  

  67.  

  68. return wpCalendar;
     

  69. }  

  70.  

  71. protected override void RenderWebPart(HtmlTextWriter output)
     

  72. {  

  73. try 

  74. {  

  75. wpCalendar.RenderControl(output);  

  76. litMsg.RenderControl(output);  

  77. roundedCorner.RenderControl(output);  

  78. }  

  79. catch (Exception ex)
     

  80. {  

  81. output.Write(ex.ToString());  

  82. }  

  83. }  

  84.  

  85. public override void RenderControl(HtmlTextWriter writer)
     

  86. {  

  87. base.RenderControl(writer);  

  88. }  

  89. }  


Note: In above code i am pulling site url and exchange server url from
the appsettings of web.config of the site.