Tuesday, February 25, 2014

How to get Guid of all form libraries

Hi Hemandra..


Thanks for the response,I am looking to relink documents using the code below...Code is working well for single form library but i have 4 to 5 form libraries for which the code should be executed..how can i get this done.I am working with Infopath forms.


protected


void btnsubmit_Click( object sender, EventArgs e)


{


if (RelinkDocuments())


{


Label3.Text =


"Success!!!" ;


}


else


{


Label3.Text =


"Failure" ;


}


}


private bool RelinkDocuments()


{


try


{


SPSite site = SPContext .Current.Site;


SPWeb spWeb = site.OpenWeb();


SPList spList = spWeb.Lists[ new Guid ( "8E023C18-DAA3-4743-81B7-E034111544BC" )];


SPDocumentLibrary doclib = ( SPDocumentLibrary )spList;


string solutionUrl = SPHttpUtility .UrlPathEncode(spWeb.Url + "/" + doclib.DocumentTemplateUrl, true );


foreach ( SPListItem spItem in doclib.Items) //loop through the list items


{


SPFieldCollection spFields = spItem.Fields;


foreach ( SPField spField in spFields) //loop through fields resetting them


{


if (spField.Type != SPFieldType .Computed &&


spField.Type !=


SPFieldType .Invalid && !spField.ReadOnlyField)


{


try


{


spItem[spField.InternalName] = spItem[spField.InternalName];


}


catch ( Exception e)


{


}


}


}


SPContentType spContentType = spList.ContentTypes[( SPContentTypeId )spItem[ "ContentTypeId" ]];


if (spContentType != null ) //try to update the item with the content type's template url which is the updated one


{


if (spContentType.DocumentTemplate.StartsWith( "http://" ) ||


spContentType.DocumentTemplate.StartsWith(


"https://" ))


{


spItem[


"TemplateUrl" ] = SPHttpUtility .UrlPathEncode(spContentType.DocumentTemplate, true );


}


else if (spContentType.DocumentTemplate.StartsWith( "/" ))


{


spItem[


"TemplateUrl" ] = SPHttpUtility .UrlPathEncode(spWeb.Site.MakeFullUrl(spContentType.DocumentTemplate), true );


}


else


{


spItem[


"TemplateUrl" ] = SPHttpUtility .UrlPathEncode(spWeb.Url + '/' + spList.RootFolder + '/' + spContentType.DocumentTemplate, true );


}


}


else //no content type found so default template url to solution Url


{


spItem[


"TemplateUrl" ] = solutionUrl;


}


spItem.Update();


}


return true ;


}


catch ( Exception ex)


{


return false ;


}


}


 


}


}





No comments:

Post a Comment