Thursday, December 5, 2013

Move a source file and its properties from document library into a folder in same document library, delete the source and replace the destination file with another file

               using (SPSite oSite = new SPSite("http://Sharepoint2010:99/TestSite"))
                {
                    using (SPWeb oWeb = oSite.OpenWeb())
                    {
                                    //source file url
                                    string url = oWeb.Url + "/" + "SourceDocLib" + "/" + "SourceFileName";
                                   string fileName = "TestFile.docs"
                                    SPFile file = oWeb.GetFile(url);
                                    byte[] fileBytes = item.File.OpenBinary();

                         /destination document library
                          SPList docLib = oWeb.Lists.TryGetList("TestDocLib");
                          SPListItemCollection listItemCollection = docLib.Items;
                          string folderName = "TestFolder";
                          //get absolute destination Url
                          string folderUrl = oWeb.Url + "/" + docLib.Title + "/" + folderName + "/" + fileName;

                foreach (SPListItem listItem in listItemCollection)
                {
                    if (listItem["Name"] != null && listItem["Name"].ToString() == fileName)
                    {
                       // copy the source file and its properties to destination location
                        listItem.CopyTo(folderUrl);
                       //delete the source item
                        listItem.Delete();
                        break;
                    }
                }
                replace the destination file with another file
                SPFile destFile = oWeb.Files.Add(folderUrl, fileBytes, true);
                destFile.Update();

People picker fields promoted from Infopath to sharepoint list as text fields

When people picker fields are promoted from infopath to sharepoint list or form library, they are created in the sharepoint as "single text line" fields. In order to have them as people picker fields,  create new people picker fields in sharepoint form library and write an event receiver and in the item adding event assign the values from the single text line fields to the people picker fields.

Retract and redeploy timer job

1) Retract the timer job using powershell conmands
2) run net stop SPTimerv4 in stsadm
3) do IISRESET in cmd
3) Deploy the timer job using powershell commands

Programmatically assigning multiple users to people picker field Sharepoint 2010

SPFieldUserValueCollection usernameCol = new SPFieldUserValueCollection();
 
SPsite osite = new SPSite(http://sharepoint2010/testing);
SPWeb oWeb = osite.OpenWeb();
SPList oList = oWeb.Lists["TestList"];
SPListItemCollection oListItemCol = oList.Items;


foreach (SPListItem items in oListItemCol)
{

SPUser user = items["PeoplePickerfield"];

if (user != null)
{
usernameCol.Add(
new SPFieldUserValue(user.ParentWeb, user.ID, user.Name));
}

}
item["MultiplevalueallowedPeoplePickerField"] = usernameCol;
item.Update();




 

Check if the file exists in the document library SharePoint 2010

using (SPSite oSite = new SPSite(http://Sharepoint2010:100/Testing))

{

using (SPWeb oWeb = oSite.OpenWeb())

{

string fileName = "Test Reports.docx";

string documentLibraryName = "Test Reports";

string fileUrl = string.Format("{0}/{1}/{2}", oWeb.Url, documentLibraryName, fileName);

SPFile file = oWeb.GetFile(fileUrl);

string workfowStatus = "Approved";

//code when all the approvers approve delete this file and move it under folder called pdf

if (file.Exists)

{
Console.WriteLine("File {0} exists in the site", file.Name);

if (workfowStatus == "Approved")

{

file.Delete();

}

}

 
else

{Console.WriteLine("File Not Found");

}

}

}

Monday, December 2, 2013

Using powershell add header and link in quick launch

param([string] $siteCollectionUrl, [string] $LeasingRelUrl)

$currentScriptName  = $MyInvocation.MyCommand.Name

$usageText = "`nUsage:`n .\"

$usageText += "$currentScriptName `"`<siteCollectionUrl>`" `"`<siteUrl>`"`n`n"

$usageText += "Where:`n"

$usageText += "siteCollectionUrl is the URL of the root site collection, e.g. http://sharepoint2010.com`n"

$usageText += "MORelUrl is the path of the site, e.g. testsite`n"

if ($siteCollectionUrl -eq "") {

write-Host -foregroundcolor Red "Required parameter siteCollectionUrl missing."

write-Host $usageText

exit

}

if ($LeasingRelUrl -eq "") {

write-Host -foregroundcolor Red "Required parameter MORelUrl missing."

write-Host $usageText

exit

}

$web = get-spweb "$siteCollectionUrl/$Url"

$pubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)

function AddHeading($HeadingName,$Link)

{

$CreateSPNavigationNode = [Microsoft.SharePoint.Publishing.Navigation.SPNavigationSiteMapNode]::CreateSPNavigationNode

$qlNav = $pubWeb.Navigation.CurrentNavigationNodes

$headingNode = $CreateSPNavigationNode.Invoke($HeadingName, $Link, [Microsoft.SharePoint.Publishing.NodeTypes]::Heading, $qlNav)

 $headingNode.Properties["Audience"] = ";;;;Group_Name"

$headingNode.Update()

$web.Update()

}

function AddLink($HeadingName,$DisplayName,$URL,$External)

{

Start-Sleep -Seconds 5

$qlNav1 = $web.Navigation.QuickLaunch

$qlNav1 | select Title, ID

$qlink = $qlNav1 | where {$_.Title -eq $HeadingName}

$linkNode = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode($DisplayName,$URL,$External)

$qlink.Children.AddAsLast($linkNode)

}

AddHeading "TestHeading" ""

AddLink "TestHeading" "TestLink" "http://hotmail.com" "True"