Wednesday, August 31, 2011

Sharepoint 2010: Folder Modified Date doesn't get updated

below is the code to update folder "ModifiedDate" when a new item is added or updated to the document library. it updates all, in case if you have more than one level of folder structure. here problem is it executes every time when you add/update any items to this Library. for example if you have 3 levels of folder structure this event calls 3 times as well.

i dont think it is a good practice...still if you have this kind of requirement from your client you can use below code.


    public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);
            if (properties.List.BaseTemplate.ToString() == "10017") // working documents
            {
                using (SPSite site = new SPSite(properties.SiteId))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        if (properties.ListItem.Url != null)
                        {
                            string listItemUrl = properties.ListItem.Url;
                            string folderPath = listItemUrl.Remove(listItemUrl.LastIndexOf('/'));
                           // string folderName = folderPath.Substring(folderPath.LastIndexOf('/') + 1);

                            SPFolder folder = web.GetFolder(folderPath);

                            if (folder != null)
                            {
                                SPListItem spitem = folder.Item;
                                if (spitem != null && spitem[SPBuiltInFieldId.Modified] != null)
                                {
                                    spitem[SPBuiltInFieldId.Modified] = DateTime.Now;
                                    spitem.Update();
                                }
                            }
                        }
                    }
                }
            }
        }
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            if (properties.List.BaseTemplate.ToString() == "10017") // working documents
            {

                using (SPSite site = new SPSite(properties.SiteId))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        if (properties.ListItem.Url != null)
                        {
                            string listItemUrl = properties.ListItem.Url;
                            string folderPath = listItemUrl.Remove(listItemUrl.LastIndexOf('/'));

                            SPFolder folder = web.GetFolder(folderPath);

                            if (folder != null)
                            {
                                SPListItem spitem = folder.Item;
                                if (spitem != null && spitem[SPBuiltInFieldId.Modified] != null)
                                {
                                    spitem[SPBuiltInFieldId.Modified] = DateTime.Now;
                                    spitem.Update();
                                }
                            }
                        }
                    }
                }
            }
        }

Tuesday, August 30, 2011

Sharepoint 2010: Hide ContentType Field using Powershell script

=================================
Hide List ContentType Field using Powershell script
======================================
$siteUrl = "http://servername/sites/P128023/"
[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint") > $null
$site = New-Object Microsoft.SharePoint.SPSite($siteUrl)
$RootWeb = $site.RootWeb

$lookForList ="Events"
$EventsList= $RootWeb.Lists[$lookForList]

if ($EventsList -ne $null)
    {
        $MissionContentType = $EventsList.ContentTypes["Mission"]
            $field = $MissionContentType.FieldLinks["Category"]
            $field.Hidden = $true
            $MissionContentType.Update();
            $EventsList.Update()
}

if ($site) {$site.Dispose()}
if ($RootWeb){ $RootWeb.Dispose()}


==========================================================
Hide ContentType Field using Powershell script
===========================================================
$siteUrl = "http://spdev5:8002/sites/library/"
[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint") > $null
$site = New-Object Microsoft.SharePoint.SPSite($siteUrl)
$RootWeb = $site.RootWeb


$MyContentType=$RootWeb.ContentTypes["CV"]
$MyField=$MyContentType.Fields["SAIC DOC"]
$MyContentType.FieldLinks[$MyField.Id].Hidden =$true
$MyContentType.Update($true)
write-host "updated" $field.type


if ($site) {$site.Dispose()}


if ($RootWeb){ $RootWeb.Dispose()}

Monday, August 29, 2011

Sharepoint 2010: Update SPNavigationNode Title

as per my requirement i need to update "Library" node to "Libraries"


Namespace:  Microsoft.SharePoint.Publishing.Navigation
Assembly:  Microsoft.SharePoint.Publishing (in Microsoft.SharePoint.Publishing.dll)
Syntax:

here is the Powershell Script :
public static SPNavigationNode UpdateSPNavigationNode(
         SPNavigationNode node,
         SPNavigationNode previous,
         string name,
         string url,
         string description,
         string target,
         string audience,
         bool forceCreate
)


$webURL="http://servername/sites/P000527/"
$web=Get-SPWeb $webURL
$navigationNode=$web.Navigation.QuickLaunch
$LibraryNode = $navigationNode | where { $_.Title -eq "Library" }
$HomeNode= $navigationHomeNode | where { $_.Title -eq "Home" }
$NewTitle= "Libraries"
[Microsoft.SharePoint.Publishing.Navigation.SPNavigationSiteMapNode]::UpdateSPNavigationNode($LibraryNode, $HomeNode, $NewTitle, $LibraryNode.Url, "", "", "", $false)
$web.Dispose()



Thursday, August 25, 2011

Sharepoint 2010 : Display All Calendar Workspaces in a Custom Page

  
 .CS File 
===========================================================
private SPWeb oWeb = null;

        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                  {
                      using (oWeb = new SPSite(SPContext.Current.Site.Url).OpenWeb())
                      {
                          //SPList EventsList = oHomeWeb.Lists["Events"];
                          SPList oCalendarList = oWeb.Lists.TryGetList("Events");
                          oWeb.AllowUnsafeUpdates = true;
                          SPViewCollection collViews = oCalendarList.Views;
                          bool isTrue = false;

                          // checking customview is avilable in the list
                          foreach (SPView view in collViews)
                          {
                              if (view.Title.Equals("CustomView"))
                              {
                                  isTrue = true;
                                  break;
                              }
                          }

                          if (!isTrue)  //create a custom view if not avilable in the list
                          {
                              string strViewName = "CustomView";
                              System.Collections.Specialized.StringCollection collViewFields = new System.Collections.Specialized.StringCollection();
                              collViewFields.Add("LinkTitle");
                              //collViewFields.Add("Location");
                              collViewFields.Add("EventDate");
                              collViewFields.Add("EndDate");
                              collViewFields.Add("OPSWorkspaceLink");

                              //string strQuery = "<Where><Gt><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Gt></Where>";
                              string strQuery = @"
                              <Where>
                                    <Eq>
                                        <FieldRef Name='WorkspaceLink' />
                                        <Value Type='CrossProjectLink'>1</Value>
                                    </Eq>
                                </Where>
                                <OrderBy>
                                    <FieldRef Name='Modified' Ascending='False' />
                                </OrderBy>
                             ";
                              collViews.Add(strViewName, collViewFields, strQuery, 50, true, false);

                          }

                          XsltListViewWebPart lvwpShowFiles = new XsltListViewWebPart();
                          lvwpShowFiles.ID = "wpListView";
                          lvwpShowFiles.ListName = oCalendarList.ID.ToString("B").ToUpper();
                          SPView oCustomView = oCalendarList.Views["CustomView"];
                          lvwpShowFiles.ViewGuid = oCustomView.ID.ToString("B").ToUpper();
                          dvSummaryView.Controls.Add(lvwpShowFiles);

                          oWeb.AllowUnsafeUpdates = false;
                      }
                  });
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {

                oWeb.Dispose();
            }
        }

 .Aspxpage  
===========================================================
    <div id="dvSummaryView"  class="ac-cnt-grey noPad"   runat="server"></div>

                     


 

Followers

Blog Archive