Revit Family Rename in a project - revit-api

Is it possible to rename families in a project through the API. We are recently updating and standardizing our family naming convention. I'd like to build an add-in that would rename the families in existing project to the new standard names. I haven't had any success finding an example of this online.
I’ve successfully been able to find all the family names, however, I can’t rename the family.
I’m using the C#.

I tried the direct way with .Name property and seems to work.
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Application app = uiapp.Application;
Document doc = uidoc.Document;
foreach(ElementId id in uidoc.Selection.GetElementIds())
{
FamilyInstance famInstance = doc.GetElement(id) as FamilyInstance;
if (famInstance == null) continue; //skip element
famInstance.Symbol.Name = famInstance.Symbol.Name + " - Changed";
}

This is what I used
Element ff = doc2.GetElement({insert family id here});
var bar = (from x in familycollection where x.Name == ff.LookupParameter("Family Name").AsString() select x).FirstOrDefault();
exEvent.Raise()

Related

Revit api import size lookup table to "Pipe Accessory" or "Plumbing Fixture"

I am trying to import loookuptabledata.csv to different families using :
Document DstnctFamEdt = actvDoc.EditFamily(DstnctFam);
FamilySizeTableManager fstm = FamilySizeTableManager.GetFamilySizeTableManager(DstnctFamEdt,DstnctFamEdt.OwnerFamily.Id);
I am able to successfully do that to "Pipe Fitting" families. However when i apply the same code to a "Pipe Accessory" family or a "Plumbing Fixture" Family, i get this error:
lookupTableResult Exception: System.NullReferenceException: Object reference is not set to an instance object.
I have tried to import a lookuptable csv file "manually" into these types of families, and Revit (2022) does not complain about it.
Any pointers on how to import loookuptable csv file into "Pipe Accessory" Families and "Plumbing Fixture" Families?
Ok, So i Figured it out....
It seems that it has nothing to do with Family Type as in if it is a Fitting, Accessory or a fixture.
What it has to do with, is whether the family have a lookuptable manager to begin with. if you happened to have csv files in your family already, the above code will work fine.
However, if you do not have any csv in your family, then you have to CREATE and new lookuptable manager, then get it.
So i modified my code the following, and it now works for all families:
Document DstnctFamEdt = actvDoc.EditFamily(DstnctFam);
// Get FamilySizeTableManager (FSTM)
FamilySizeTableManager fstm = FamilySizeTableManager.GetFamilySizeTableManager(DstnctFamEdt,
DstnctFamEdt.OwnerFamily.Id);
// if FSTM is NUll then it does not exist=> Create one
if (fstm == null) {
bool fstmResult = FamilySizeTableManager.CreateFamilySizeTableManager(DstnctFamEdt, DstnctFamEdt.OwnerFamily.Id);
// Check that you actually created and FSTM, and Retrieve it.
if (fstmResult) {
fstm = FamilySizeTableManager.GetFamilySizeTableManager(DstnctFamEdt, DstnctFamEdt.OwnerFamily.Id);
}
}
// If FSTM Does Exit then Load csv File and Save Family and load into original DOC that family exists in.
if (fstm != null) {
// Import the csv file into Family... transaction needed
Transaction importTrans = new Transaction(DstnctFam.Document, "Importing csv File into Family ");
importTrans.Start("Start");
FamilySizeTableErrorInfo errorInfo = new FamilySizeTableErrorInfo();
FamilySizeTableErrorInfo ImportErrorInfo = new FamilySizeTableErrorInfo();
fstm.ImportSizeTable(DstnctFamEdt, csvFilePath, ImportErrorInfo);
// End transaction
importTrans.Commit();
// Load Family into Doc where it exists after adding csv file.
DstnctFamEdt.LoadFamily(actvDoc, new familyLoadOptions()); //update begins here
SaveAsOptions saveoptions = new SaveAsOptions();
saveoptions.OverwriteExistingFile = true;
// Save the Family if you want to, otherwise, make sure you save the Host doc.
DstnctFamEdt.SaveAs(DstnctFamPath, saveoptions);
// Cloes Family.
DstnctFamEdt.Close(false);
Ok, let me know if anyone can add to this.
thanks

Create Document Library using Client Object Model

Just a quick question, is it possible to create a Document Library using the Client Object Model in SharePoint 2010?
I know you can create lists etc. Am I right in saying that a Document Library is just a 'special' type of List?
How can I specify that the List I create will be a Document Library?
Any help will be appreciated
Thanks
Yes. You can specify the TemplateType for your List.
List of TemplateTypes
using (ClientContext clientCTX = new ClientContext(url))
{
ListCreationInformation lci = new ListCreationInformation();
lci.Description = "My own DocLib";
lci.Title = "Library";
lci.TemplateType = 101;
List newLib = clientCTX.Web.Lists.Add(lci);
clientCTX.Load(newLib);
clientCTX.ExecuteQuery();
}
You can also set the templatetype with the following code, which I think makes things clearer to new programmers:
lci.TemplateType = (int) ListTemplateType.DocumentLibrary;

SharePoint 2010 BaseFieldControl Duplicates first item in list when in DisplayMode

I have come across a problem when using the BaseFieldControl that is driving me to distraction.
Essentinally I am trying to convert a list into a HTML table using the BaseFieldControl to render the fields.
When my table renders it writes out the correct number of lines BUT the data in each line is always the same as the first item in the list.
When I change the ControlMode property from SPControlMode.Display to SPControlMode.Edit the list renders correctly ( apart from being in Edit mode )
When my code running with ControlMode set to SPControlMode.Display I can actually get at the correct value in the BaseFieldControl.ItemFieldValue property but the wretched BaseFieldControl still insists on rendering the first item in the list!
I've also installed the web part on a SharePoint foundation and SharePoint 2010 server and I get the same results!
Finally I've googled around and found other peoples examples. Unfortunately when I try other dev's code ( unmodified ) I get exactly the same results!
This is what I'm doing. Any suggestions would be really appreciated!
foreach (string f in list.DefaultView.ViewFields)
{
TableCell c = new TableCell();
var i = item[f];
if (i != null)
{
SPField spf = item.Fields.GetField(f);
BaseFieldControl bfc = spf.FieldRenderingControl;
bfc.ControlMode = SPControlMode.Display;
bfc.Value = bfc.ItemFieldValue;
bfc.ID = Guid.NewGuid().ToString();
bfc.FieldName = spf.Title;
bfc.ListId = list.ID;
bfc.ItemId = item.ID;
SPContext context = SPContext.GetContext(this.Context, item.ID, list.ID, SPContext.Current.Web);
bfc.ItemContext = context;
bfc.RenderContext = context;
bfc.EnableViewState = true;
bfc.Visible = true;
c.Controls.Add(bfc);
}
else
{
c.Text = "NULL";
}
r.Cells.Add(c);
}
I finally fixed it. Turns out to be a problem with the SPWeb object. I had grabbed it from SPContext and passed it through as a reference to my method.
When I stopped doing that but instead created it within the method ( and created it once per item in the list ) it all worked fine.
Very strange.

Sharepoint 2010 - make Title, Description and Keyword fields as required fields in Picture Library using server-object-model

I'm creating a Sharepoint feature, this feature has an event receiver associated to it. In the event receiver, I'm creating a Document Library and Picture Library using server-side object model. I'm also adding new custom columns (around 80) to these newly created document and picture library. Now I want to modify the properties of the Description, Keywords and Title fields that are by default created along with the picture library. I want to make these fields as Required fields. How do I do this? I tried to set SPList.AllowContentTypes = true and try to change the attributes of these fields, but it doesn't work (neither gives an error nor makes these required fields). I also tried to access the content types and try to change the attributes using SPContentType.FieldsLinks["Column_name"].Required and SPContentType.Fields["Column_name"].Required but it gives me an error. Does anyone have any other suggestions?
Here is the answer....
SPContentType ct = mypiclib.ContentTypes["Picture"];
SPFieldLinks titleLink = ct.FieldLinks["Title"];
SPFieldLinks descLink = ct.FieldLinks["comments"]; //internal name of Description
SPFieldLinks keywords = ct.FieldLinks["keywords"];
titlelink.Required = true;
descLink.Required = true;
keywords.Required = true;
ct.Update();
can you tell us the error you got when trying to use the fieldlinks? Because this should work... I have done it like this:
SPContentType ct = web.Lists["*ListName*"].ContentTypes["*ContentTypeName*"];
SPFieldLinkCollection flinks = ct.FieldLinks;
flinks["*ColumnName*"].Required = true;
ct.update();
This should do the trick:
SPWeb yourWeb = ... //assign your web
SPList yourPictureLibrary = ... //assign your picture library
web.AllowUnsafeUpdates = true;
yourPictureLibrary.Fields[SPBuiltInFieldId.Title].Required = true;
yourPictureLibrary.Fields[SPBuiltInFieldId.Description].Required = true;
yourPictureLibrary.Fields[SPBuiltInFieldId.Keywords].Required = true;
yourPictureLibrary.Update();
SPAllowContentTypes isn't settable. You might try setting ContentTypesEnabled instead.
I don't have a 2010 box to test against, but if SPAllowContentTypes returns false I think you're looking at modifying the definition of your picture library in the 14 hive (TEMPLATE\FEATURES\PictureLibrary\PicLib) to get what you're after. Tread lightly there.

Enterprise Keyword not updating in SharePoint 2010

Any idea how to inject values to the Enterprise Keywords column of a List / Doc Lib Item using code?
Tried the following, it didn't give any error, but that column wouldn't update, while the Title did.
using (var site = new SPSite("http://testweb"))
{
using (var web = site.OpenWeb("testsite1"))
{
var list = web.Lists["testlist1"];
var item = list.AddItem();
item["Title"] = string.Format("Injected from code on {0}", DateTime.Now.ToString());
item["Enterprise Keywords"] = "1;#Opera|4eed0518-9676-4afc-be20-9027b3b69e42";
item.Update();
}
}
In this code, Opera keyword has been added previously, I've checked it against the TaxonomyHiddenList list as well using code to extract the correct ID and IdForTerm (the GUID).
What am I missing here?
To add a taxonomy field value the approach is a little bit different. Please try:
TaxonomyField entKeyword = (TaxonomyField)item.Fields["Enterprise Keywords"];
TaxonomyFieldValue term = new TaxonomyFieldValue("1;#Opera|4eed0518-9676-4afc-be20-9027b3b69e42");
entKeyword.SetFieldValue(item,term);
in stead of:
item["Enterprise Keywords"] = "1;#Opera|4eed0518-9676-4afc-be20-9027b3b69e42";

Resources