September 4, 2013

Get User's Properties from Active Directory through profile synchronization.

​User's Property in Active directory are there like City, Job, Birthday, Hire Date etc.
what i have done here is displaying current week's birthday(i.e Sunday to satureday) and hire date with total years of joining from active directory through userprofile synchronization.
Before i write code here, make sure that user profile does contains properties which you need, If it is not there than add that property. Also user must be there, user profile synchronization service must be started

   1:  SPSecurity.RunWithElevatedPrivileges(delegate()
   2:              {
   3:                  using (SPSite site = new SPSite(SPContext.Current.Site.RootWeb.Url))
   4:                  {
   5:                      using (SPWeb web = site.OpenWeb())
   6:                      {
   7:                          try
   8:                          {
   9:   
  10:   
  11:                              SPServiceContext spServiceContext = SPServiceContext.GetContext(site);
  12:                                                                                  
  13:   
  14:   
  15:                              UserProfileManager profileManager = new UserProfileManager(spServiceContext);
  16:                              if (profileManager == null)
  17:                                  return;
  18:                              
  19:                              startDate = DateTime.Now;
  20:                              while (startDate.DayOfWeek != DayOfWeek.Sunday)
  21:                              {
  22:                                  startDate = startDate.AddDays(-1);
  23:                              }
  24:                              endDate = startDate.AddDays(6);                            
  25:                              startdate = startDate.Day;
  26:                              startmonth = startDate.Month;
  27:                              enddate = endDate.Day;
  28:                              endmonth = endDate.Month;
  29:                              IEnumerator profileEnumerator = profileManager.GetEnumerator();
  30:                              while (profileEnumerator.MoveNext())
  31:                             
  32:                              {
  33:                                  UserProfile userProfile = profileEnumerator.Current as UserProfile;
  34:                                  if (SPContext.Current != null)
  35:                                  {
  36:                                      string userLoginName = userProfile["AccountName"].ToString();
  37:                                                                                               
  38:                                  }
  39:                                  if (userProfile == null)
  40:                                      return;
  41:                                  DataRow drbirthday = dtBirthday.NewRow();
  42:                                  DataRow drAnniversary = dtAnniversary.NewRow();
  43:                                  if (userProfile[PropertyConstants.Birthday].Value != null)
  44:                                  {
  45:                                      BirthdayDate = (DateTime)userProfile[PropertyConstants.Birthday].Value;
  46:                                      if ((BirthdayDate.Day >= startdate && BirthdayDate.Month == startmonth) && (BirthdayDate.Day <= enddate && BirthdayDate.Month == endmonth))
  47:                                      {
  48:                                          drbirthday["Name"] = userProfile[PropertyConstants.PreferredName].Value.ToString();
  49:                                          drbirthday["Date"] = BirthdayDate.Month + "/" + BirthdayDate.Day;
  50:                                          drbirthday["fullstring"] = Convert.ToString( drbirthday["Date"]) + "-" + Convert.ToString( drbirthday["Name"]);
  51:                                          dtBirthday.Rows.Add(drbirthday);
  52:                                          birthDayData.Text = birthDayData.Text + (birthDayData.Text == string.Empty? string.Empty: "; ") +Convert.ToString(drbirthday["fullstring"]);
  53:                                      }                                   
  54:                                  }
  55:                                  if (userProfile[PropertyConstants.HireDate].Value != null)
  56:                                  {
  57:                                      AnniversaryDate = (DateTime)userProfile[PropertyConstants.HireDate].Value;
  58:                                      if ((AnniversaryDate.Day == date && AnniversaryDate.Month == month ))
  59:                                      {
  60:                                          int todayyear = DateTime.Today.Year;
  61:                                          int Anniyear = AnniversaryDate.Year;
  62:                                          int totalAnniYear = todayyear - Anniyear;
  63:                                          drAnniversary["Name"] = userProfile[PropertyConstants.PreferredName].Value.ToString();
  64:                                          drAnniversary["Years"] = totalAnniYear;
  65:                                          drAnniversary["fullstring"] = Convert.ToString(drAnniversary["Name"]) + "-" + Convert.ToString(drAnniversary["Years"] + " years");
  66:                                          dtAnniversary.Rows.Add(drAnniversary);
  67:                                          AnniDayData.Text = AnniDayData.Text + (AnniDayData.Text == string.Empty? string.Empty: ", ") + Convert.ToString(drAnniversary["fullstring"]);
  68:                                      }                                    
  69:                                  }
  70:                              }
  71:   
  72:   
  73:                          }
  74:                          catch (Exception ex)
  75:                          {
  76:                              Common.ErrorLog(ex.Message + Environment.NewLine + ex.StackTrace, "GetUserBirthdayAnniversary", Microsoft.SharePoint.Administration.TraceSeverity.Unexpected);
  77:                          }
  78:   
  79:   
  80:                      }
  81:                  }
  82:              });
This will helps to get user's properties from Active Directory.

If you have any questions you can reach out our SharePoint Consulting team here.

Unable to load workflow actions from server SharePoint Designer 2010

Problem:

When trying to open any workflow in SharePoint Designer, Error gets fired like:

Unable to load workflow actions from the server….

While checking ULS logs, below exception was found:

SOAP exception: System.ArgumentException: An item with the same key has already been added. at
System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) at
System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) at
System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value) at
Microsoft.SharePoint.SoapServer.WebPartPagesWebService.GetWorkflowActionsFromFeatureElements(UInt32 currentLcid) at
Microsoft.SharePoint.SoapServer.WebPartPagesWebService.FetchLegalWorkflowActions()

Looking to exception details it becomes clear that this error is something related with duplication.

Solution:

Check Farm Level Solutions:
Browse to Central administration ->Click System Settings ->Click Manage Farm Solutions.

Then,

Check Site Collection Level Solutions:
Browse to Site collection ->Click Site Actions ->Site Settings ->Solutions (under Gallery).

Put both of the browser windows side-by-side and see if you have anything activated at both scopes, means there is any solution which is deployed at farm level and the same solution is also uploaded and activated to solutions gallery at site collection level. If you find this, deactivating the particular solution at site collection level should do the trick to open workflows in SharePoint Designer.


If you have any questions you can reach out our SharePoint Consulting team here.

August 28, 2013

List all pages using specific Page Layout

clear
   1:  Add-PsSnapin Microsoft.SharePoint.PowerShell
   2:  filter Get-PublishingPages
   3:  {
   4:                  $pubweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($_)
   5:      $query = new-object Microsoft.SharePoint.SPQuery
   6:                  $query.Query = "<Where><In><FieldRef Name='PublishingPageLayout' /><Values><Value Type='URL'>/_catalogs/masterpage/<Layout1.aspx></Value><Value Type='URL'>/_catalogs/masterpage/<Layout2.aspx></Value><Value Type='URL'>/_catalogs/masterpage/<Layout3.aspx></Value></Values></In></Where>"
   7:     
   8:                  $query.ViewAttributes = "Scope='Recursive'"
   9:      $pubweb.GetPublishingPages($query)   
  10:  }
  11:   
  12:  $str = Read-Host "Enter Site URL: "
  13:   
  14:  if($str -eq $null )
  15:  {
  16:                  Write-Host "Enter a valid URL"
  17:                  return
  18:  }
  19:   
  20:  $site = Get-SPSite -Identity $str
  21:  if($site -eq $null)
  22:  {
  23:                  Write-Host "Enter a valid URL"
  24:                  return
  25:  }
  26:   
  27:  $allweb = $site.Allwebs
  28:  foreach($web in $allweb )
  29:  {
  30:      $web | Get-PublishingPages | select Uri, Title, @{Name='PageLayout';Expression={$_.Layout.ServerRelativeUrl}}| Format-List
  31:  }

If you have any questions you can reach out our SharePoint Consulting team here.