Update Lookup Column Web and List Programmatically

I had requirement of creating custom list programmatically for our varience sites en-us(English) and de-de(German). For that, I have created site-columns, content-type and add site-columns to content-type at root site level. Now I am using same content-type in varience site’s list. But the lookup column of varience site’s list refer to root web ( and list) instead of current web.So to fix this issue I have to manually change the lookup column webid and list of current site not root site.

 

///
/// Set lookup site-columns webid and listid for all variance site
///
public static void SetLookupFieldWebAndListID()
{
  try
  {
    using (SPSite site = new SPSite(RootSiteUrl))
    {
      SPWebCollection allWebs = site.AllWebs;
      foreach (SPWeb currentWeb in allWebs)
      {
        if (PublishingWeb.IsPublishingWeb(currentWeb))
        {
          using (currentWeb)
          {
            //fetch lookup column list and lookup column
            SPList list = currentWeb.Lists["ListName"];
            SPFieldLookup lookupColumn = (SPFieldLookup)list.Fields.GetField("LookupColumnName");

            //update lookup column webid
            lookupColumn.LookupWebId = currentWeb.ID;

            //fetch lookup list
            SPList lookupList = currentWeb.Lists["lookupListName"];

            //update lookup column listid
            if (string.IsNullOrEmpty(lookupColumn.LookupList))
            {
              lookupColumn.LookupList = lookupList.ID.ToString();
            }
            else
            {
              lookupColumn.SchemaXml = lookupColumn.SchemaXml.ReplaceXmlAttributeValue("List", lookupList.ID.ToString()).ReplaceXmlAttributeValue("WebId", currentWeb.ID.ToString());
            }
            lookupColumn.Update(true);
          }
        }

      }
    }

  }
  catch (Exception ex)
  {
    Logger.LogError(ex.Message, "Setup lookup field error", ex.StackTrace);
  }
}

///
/// Relpace the WebID and ListID of lookup field xml-schema.
///
public static string ReplaceXmlAttributeValue(this string xml, string attributeName, string value)
{
            if (string.IsNullOrEmpty(xml))
            {
                throw new ArgumentNullException("xml");
            }
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentNullException("value");
            }

            int indexOfAttributeName = xml.IndexOf(attributeName, StringComparison.CurrentCultureIgnoreCase);
            if (indexOfAttributeName == -1)
            {
                throw new ArgumentOutOfRangeException("attributeName", string.Format("Attribute {0} not found in source xml", attributeName));
            }

            int indexOfAttibuteValueBegin = xml.IndexOf('"', indexOfAttributeName);
            int indexOfAttributeValueEnd = xml.IndexOf('"', indexOfAttibuteValueBegin + 1);

            return xml.Substring(0, indexOfAttibuteValueBegin + 1) + value +    xml.Substring(indexOfAttributeValueEnd);
}

 

Steps to reflect changes in ShaPoint Timer job in MOSS

 

I’ve developed SharePoint Timer job in moss and gained a lot of experiences with deploying, configuring, debugging and updating it. The problem I had faced was my timer job changes don’t affect. Even after deploying multiple times then also changes are not affect in timer job. After lots of efforts finally I got the steps which will reflect the changes in timer job every time I deploy the solution. Here is the steps (my project type is WSPBuilder):

 

First Way:

  1. In visual studio 2008 right click on solution, Select WSPBuilder -> Uninstall.
  2. Check the dll of your solution name in C:WINDOWSassembly and if you find your dll then right click on dll and select Uninstall.
  3. Restart Windows SharePoint Services Timer from the Services.
  4. iisreset.
  5. Right click on solution, select Build.
  6. Right click on FEATURES folder of solution and select Copy to 12 hive.
  7. Right click on solution, WSPBuilder -> Copy to GAC.
  8. Right click on solution, WSPBuilder -> Copy to 12 hive.
  9. Right click on solution, WSPBuilder -> Build WSP.
    1. Right click on solution, WSPBuilder -> Deploy.
    2. Now you can run you timer job. You will got the changes.

 

Second Way:

After following above steps sometimes you might got changes in timer job. For that follow the below steps:

  1. Make changes in code which you want to do in code.
  2. After that right click on solution, WSPBuilder -> Upgrade. One popup is come to you for Rebuild Solution, click on Yes button.
    Timerjob
    Note: If you do not get this popup then your code might not be changed. So, for that again make some changes in you code and try again Step no.1 and 2 until you get this popup.
  1. Right click on solution, select Clean.
  2. Right click on solution, select Rebuild.
  3. Follow the same steps from First way (1 to 10).

 

Reference links

http://bytes.com/topic/asp-net/insights/902681-how-create-deploy-sharepoint-2007-timer-job

 

Sameer J. Vaghela

 

Moss 2007 to Sharepoint2013 page migration

Few months ago, I had arequirement of copy SharePoint 2007 pages to SharePoint 2013 site in my migration project. The web application was hosted on other server (not on the SharePoint server). So Ihad to use SharePoint services model.I have used copy-service for this requirement. Please note that this service will only copy static content of page not other webparts content.

To start the page migration, first fetch source page fieldinfo and bytearray using copy-service ‘GetItem’ method(add copy service reference and programatically change its url).

 //give source copy-service url and credentials
CopyWS.Copy copyWS = new CopyWS.Copy();
copyWS.Url = “http://wssdev:7001/_vti_bin/copy.asmx”;
copyWS.Credentials = new NetworkCredential(“sourcesite-username”, “sourcesite-password”);//fetch page fieldinfo and bytearray using copy GetItem method from source page.
CopyWS.FieldInformation[] mossPageFieldInfoArray;
byte[] mossPageByteArray;
uint myGetUint = copyWS.GetItem(“http://wssdev:7001/Pages/test1.aspx”, out mossPageFieldInfoArray, out mossPageByteArray);

Next, set copy-service url to destination site and copy the page using ‘CopyIntoItems’ method. Provide source page fieldinfo and bytearray into this method.

//give destination copy-service url and credentials
copyWS = new CopyWS.Copy();
copyWS.Url = “http://spe2013dev1:4444/_vti_bin/copy.asmx”;
copyWS.Credentials = new NetworkCredential(“destinationsite-username”, “destinationsite-password”);// create page in destination site using source page info.
CopyWS.CopyResult[] copyResultArray;
string[] copyDest = { “http://spe2013dev1:4444/Pages/test1.aspx” };
uint myCopyUint = copyWS.CopyIntoItems(“http://wssdev:7001/Pages/test1.aspx”, copyDest, mossPageFieldInfoArray, mossPageByteArray, out copyResultArray);

To confirm the migration process, check CopyResult.ErrorCode, if it is successful than your page gets copied successfully else it will show error message.

Additional Information:

  • Page FieldInformation Array
  • Page Byte Array
  • Page Copy Result
Related link:
http://msdn.microsoft.com/en-us/library/copy.copy.copyintoitems%28v=office.12%29.aspx

Working with SharePoint Search with Form Based Authentication

When you configure form based authentication sometime is is need that  you disable Integrated Windows Authentication. By doing so search does not work as per expectation and you may face error. If you check in crawl log you will saw following notification error message.

Error:

Access is denied. Verify that either the Default Content Access Account has access to this repository, or add a crawl rule to crawl this repository. If the repository being crawled is a SharePoint repository, verify that the account you are using has “Full Read” permissions on the SharePoint Web Application being crawled. ( HttpStatusCode Unauthorized The request failed with HTTP status 401: Unauthorized. )

Reason:

This error will occur, when search engine tries to crawl the content using the URL but as there is a change in user identity server is unable to access database and server will reject the request.

Solution:

For SharePoint Search you need Integrated Windows Authentication for crawling the content database. To resolve this issue we need to create extended website. Follow below steps to extend

  1. Go to Central Administration and select your Form Based Authentication website and extend your website with integrated windows authentication enabled. Note, when you extend website you have different site in IIS.
  2. Now modify your search service application’s content source and in content source give your extended website URL instead of live site url(Form-based authenticated site).
  3. Once you do that, click on content source and start full crawl. After crawl completes you will not face above error and search result is coming.
  4. If you check in your site now you may observe that search result is coming with extended site URL not with live site URL. To display live site URL open your Search Service Application and in the crawling section click on Server Name Mappings.
  5. From Server Name Mappings click on New Mapping.
  6. “In Address in index” give your extended site URL and “In address in search results” give your live site URL and click on Ok.
  7. Now once again click on content source and start full crawl. Now result will come with your live site URL.

 

Shraddha Shah
SharePoint Developer

Error (The method "GetItems" of the type "List" with id is blocked by the administrator on the server ) when accessing client object model in public facing website.

 

We might have seen this error several time. (The method “GetItems” of the type “List” with id is blocked by the administrator on the server )Here is my scenario. We had launched a public facing website built in SharePoint 2010. Basically this error occurs when site has anonymous access and where Client Object Model is used for accessing list on the site.

To fix this problem perform following steps.

  • Open SharePoint 2010 management shell and run following commands

    $webapp = Get-SPWebApplication “your site url”
    $webapp.ClientCallableSettings.AnonymousRestrictedTypes.Remove([microsoft.sharepoint.splist], “GetItems”)
    $webapp.Update()

To know more about  “ClientCallableSettings”  visit this link
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spclientcallablesettings.aspx

Shraddha Shah
SharePoint Developer